Skip to content

App

Dashboards

A recipe for a dashboard built only from ogygia's own pieces, with no store library. The shell stays server HTML, each tile is an island or a server island, one wired class holds the filters every tile reads, and remote functions keep the data fresh.

On this page

A static page can’t change after it loads, and a dashboard has to follow its filters. An SPA ships and hydrates JavaScript for every tile, even the ones that are mostly read-only. With ogygia it splits along island lines: the shell is server HTML, each tile wakes on its own, and one shared object holds the filters.

This page adds no new API. It shows which piece owns which job, and links to the page that explains each one.

Dashboard pieceogygia piece
Shell: nav, headers, layoutPlain page markup. It ships no JavaScript.
Interactive tile: a chart with tooltips, a sortable tableClient island (wake)
Read-only tile: personal or per-request HTMLServer island (render: 'deferred')
Heavy static content inside a tileLake (wake: 'none')
Global filters: date range, tenant, segmentOne wired class set through context
Tile dataRemote functions called inside the tile
Ticking tile: live numbers, a feedquery.live yielding held regions
A tile the server picksHeld region

The sections below build a dashboard from the top down. It uses remote functions and top-level await in components, so turn on kit.experimental.remoteFunctions and Svelte’s compilerOptions.experimental.async in svelte.config.js, and add transport to src/hooks.ts.

Shared filters

When you change the date range, every tile should follow. Model the filters as one wired class, and put all shared UI state on it, including the segment a click on a chart selects. A typed context hands it to every tile.

Set it once in the page, above the tiles. The page’s script runs only on the server, so it creates one instance per request, and nothing is shared between visitors.

Each tile is a separate bundle, but they all hold one live object. When the picker writes filters.range, every tile that read it updates, with no props threaded through and no event bus. A visible tile that wakes later joins the current instance, selections included. State and context explains how.

import { createContext } from 'ogygia';

export class Filters {
  range = $state('30d');
  segment = $state<string | null>(null);

  static wire = import.meta.og.wire({
    encode: (f: Filters) => ({
      range: f.range,
      segment: f.segment
    }),
    decode: (d: { range: string; segment: string | null }) =>
      Object.assign(new Filters(), d)
  });
}

export const [getFilters, setFilters] = createContext<Filters>(
  'dashboard-filters'
);
<script>
  import RangePicker from '$lib/dashboard/RangePicker.svelte' with {
    wake: 'load'
  };
  import Revenue from '$lib/dashboard/Revenue.svelte' with {
    wake: 'load'
  };
  import TopProducts from '$lib/dashboard/TopProducts.svelte' with {
    wake: 'visible'
  };
  import {
    Filters,
    setFilters
  } from '$lib/dashboard/filters.svelte';

  setFilters(new Filters());
</script>

<RangePicker />
<div class="grid">
  <Revenue />
  <TopProducts />
</div>
<script>
  import { getFilters } from './filters.svelte';
  const filters = getFilters();
</script>

<select bind:value={filters.range}>
  <option value="7d">Last 7 days</option>
  <option value="30d">Last 30 days</option>
  <option value="90d">Last 90 days</option>
</select>

Tile data

Each tile calls its own query. Derive the call from the shared filters, and the tile fetches again by itself when they change.

This tile gets four things without extra code.

  • The first paint has the number. The await runs in the server render, and in a production build its result is seeded into the page, so the tile hydrates without fetching again. In dev, the tile fetches again when it wakes.
  • No refetch wiring. $derived calls the query again when filters.range changes. That’s plain Svelte.
  • No spinner on every change. While the new call is out, Svelte keeps the old number on screen, and stats.loading dims the tile.
  • Shared data is fetched once. Two tiles that call getStats('30d') share one request and one cache entry, because every island on the page shares one client runtime. Put an expensive aggregate in one query and let the number, the chart and the table all read it.

Cross-filter with a field write

A click on a chart bar sets filters.segment, and TopProducts passes filters.segment to its query. The two tiles know nothing about each other: the shared object coordinates them.

Render it inside a tile, such as Revenue, so it runs in that island and can read the filters.

import { query } from '$app/server';
import * as v from 'valibot';
import { revenueFor, topProducts } from '$lib/server/metrics';

export const getStats = query(v.string(), (range) =>
  revenueFor(range)
);

export const getTopProducts = query(
  v.object({
    range: v.string(),
    segment: v.nullable(v.string())
  }),
  ({ range, segment }) => topProducts(range, segment)
);
<script>
  import { getStats } from './dashboard.remote';
  import { getFilters } from './filters.svelte';

  const filters = getFilters();
  const stats = $derived(getStats(filters.range));
</script>

<article class:stale={stats.loading}>
  <h3>Revenue</h3>
  <strong>{(await stats).revenue}</strong>
</article>

<style>
  .stale { opacity: 0.6; transition: opacity 150ms; }
</style>
<script>
  import { getTopProducts } from './dashboard.remote';
  import { getFilters } from './filters.svelte';

  const filters = getFilters();
  const top = $derived(
    getTopProducts({
      range: filters.range,
      segment: filters.segment
    })
  );
</script>

<ul class:stale={top.loading}>
  {#each await top as product}
    <li>{product.name}</li>
  {/each}
</ul>
<script>
  import { getFilters } from './filters.svelte';
  let { buckets } = $props();
  const filters = getFilters();
</script>

{#each buckets as bucket, i}
  <rect
    x={i * 24}
    y={100 - bucket.value}
    width="20"
    height={bucket.value}
    onclick={() =>
      (filters.segment = bucket.name)}
  />
{/each}

Server tiles

Not every tile needs JavaScript. A “recent orders” list nobody interacts with can be a server island: HTML rendered per request, with no client code. A tile below the fold can wait until it scrolls into view before it costs the server a render.

On a full page load each server island is its own request. On a client navigation, single-flight navigation fetches all the load ones in one request.

A server island renders on its own endpoint, so it doesn’t see the page’s context and doesn’t react to the filters. Its props are fixed when the page renders. A tile that has to follow the filters is a client island with a query, as above.

Keep heavy content out of a tile’s bundle

An interactive tile can hold a big server-rendered SVG chart. Mark the chart’s import wake: 'none' inside the tile, and it becomes a lake: the tile’s controls hydrate, and the chart’s code never enters the client bundle. To refresh it, mark it render: 'live' with a wake schedule (say wake: 'idle') instead of wake: 'none', and the lake fetches fresh HTML when the tile mounts it again (see Keep a lake fresh).

Let the server pick the tiles

On a dashboard visitors arrange, the server knows the layout, so the browser needs no widget registry. A remote returns a region(…) for each slot, the page renders <Region of={…}> for whatever comes back, and the browser never downloads a widget the visitor didn’t place. See Held regions.

<script>
  import RecentOrders from '$lib/dashboard/RecentOrders.svelte' with {
    render: 'deferred'
  };
  import AuditLog from '$lib/dashboard/AuditLog.svelte' with {
    render: 'deferred',
    fetch: 'visible'
  };
</script>

<RecentOrders />
<AuditLog />

Live data

For data that ticks, like active users or a status feed, a query.live pushes updates. Yield a held region on each tick, and the server renders the update.

A region with no schedule morphs in place on each tick, so focus and open popovers stay. An interactive one stays mounted and gets the new props, so a hovered point or an expanded row survives. Live regions covers both.

Use query.live only for data that changes on its own. Data that changes because the visitor acted is cheaper as a plain query that $derived calls again.

import { query } from '$app/server';
import { region } from 'ogygia';
import * as v from 'valibot';
import LiveStat from './LiveStat.svelte' with { region: 'raw' };
import { watchActiveUsers } from '$lib/server/metrics';

export const activeUsers = query.live(
  v.string(),
  async function* (range) {
    for await (const n of watchActiveUsers(range))
      yield region(LiveStat, { n });
  }
);
<script>
  import { Region } from 'ogygia';
  import { activeUsers } from './live.remote';
  import { getFilters } from './filters.svelte';

  const filters = getFilters();
</script>

<Region of={activeUsers(filters.range).current} />

Drill-down

Dashboards navigate: from the overview to orders to one order. With the SPA router those are real routes with URLs, a working Back button and load functions, not modal state.

  • Filters can follow the visitor. Give the codec an id, and the same live Filters instance follows the visitor from page to page within the tab, so the detail page opens with the range they chose. Without an id, each page starts fresh. For dashboards people share by link, keep the filters in the URL and read them in load. See Keep state across navigations.
  • An expensive tile can come along. with { wake: 'load', keep: 'chart' } moves the live island into the next page instead of mounting it again, so a chart keeps its zoom and its fetched data, and the next page’s props are pushed in. See Client islands.
  • Half-filled forms come back. A settings form left mid-edit is filled in again when the visitor returns to it in the same tab. See Half-filled forms.

Built-in parts

If you find yourself writing one of these, a piece above already does it:

If you’re writing…Use instead
A store library or an event bus between tilesOne wired class through context
An effect that refetches when a filter changes$derived(query(filters.x))
A layer to share data between tilesOne query, many readers
Spinner state per tileawait for the value, .loading to dim
WebSocket code plus diffing for live tilesquery.live that yields held regions
A client-side widget registry for visitor layoutsHeld regions: the server picks, the browser paints
Lazy tile loadingwake: 'visible', or render: 'deferred', fetch: 'visible'
Code to keep a chart alive across pageskeep

Do and don’t

  • Do put every piece of shared UI state (range, segment, selection) on the one wired class. Two sources of truth make tiles disagree.
  • Do use wake: 'visible' for tiles and fetch: 'visible' for server islands below the fold.
  • Do dim on .loading instead of swapping to a spinner.
  • Don’t make a tile a server island because it feels server-side. If it reacts to filters, it’s a client island with a query.
  • Don’t judge data flows in dev, where query results aren’t seeded. Check a production build before you judge a flash.