Regions
Live regions
Send a held region again when the data behind it changes. Stream new versions with query.live, repaint from a command's own response, keep the newest answer with a version, and refresh a site header without a navigation.
A held region is a value, so the server can send it again. This page covers the ways a region already on the page gets new HTML: a query.live that sends it on every change, a command that sends it back with its answer, and a query you refresh. Each one repaints the region in place, so the islands inside it keep their state.
Two names sound alike here. A live region is a held region that a query.live sends again and again, covered below. A live lake is a lake marked render: 'live', which fetches fresh HTML when its island mounts it again; Lakes covers that one.
The smallest case is a query.live that yields a region each time the data changes:
import { query } from '$app/server';
import { region } from 'ogygia';
import * as v from 'valibot';
import StatCard from '$lib/StatCard.svelte' with {
region: 'raw'
};
import { watchStats } from '$lib/server/stats';
export const stats = query.live(
v.string(),
async function* (id) {
for await (const s of watchStats(id))
yield region(StatCard, { stats: s });
}
);Streaming with query.live
A query.live can send a held region again and again. yield a region on each tick, as in the example above, and its rendered HTML rides the live connection you already have, with no fetch per tick.
<Region of={…current}> shows the first tick, then follows. A region with no schedule (region: 'raw') morphs in place on each tick, so focus and typed text survive. An interactive one stays alive: its new props are pushed into the mounted island, and its own state stays. Either way, one spot on the page shows the latest tick. For a growing feed, collect the ticks into an array yourself.
The live connection runs in the browser, so Stats has to be an island. The page marks its import wake.
<script>
import { Region } from 'ogygia';
import { stats } from '$lib/stats.remote';
let { id } = $props();
</script>
<Region of={stats(id).current} /><script>
import Stats from '$lib/Stats.svelte' with {
wake: 'load'
};
</script>
<Stats id="main" />Command repaints
A command that returns a held region sends the re-rendered HTML in its own response. Any <Region> on the page that shows the same region updates from it, with no follow-up fetch. One round trip changes the data and repaints.
“The same region” means the same component with the same props, because that pair is the region’s address. So keep the props to what names the thing (an id, or nothing at all) and let the component read the data when it renders. If the props carried the count itself, the command’s region would have new props, so it would be a different region and nothing on the page would match it.
In the example, badge and bump both return region(Badge, {}). BadgePanel is an island, because its button runs in the browser. It places the query’s region and calls the command, and the badge repaints from the command’s answer. The demo below does the same thing.
Badge reads the count from $lib/badge-count, not $lib/server. A region: 'raw' component is still built into a client chunk (loaded only if a region() call gives it a wake), so Kit refuses a $lib/server import in it.
Order answers with a version
Two commands that return the same region can answer in either order. The browser can’t tell which answer has newer data, so by default the last one to arrive stays on screen, even if it was rendered from older data. Pass the data’s version as the third argument of region(), as { version }, and the browser keeps the newest data instead:
- An answer older than the one on screen is dropped. In dev, ogygia says so (
region_answer_older). - Equal versions, or an answer without a version, fall back to arrival order.
- The version must go up whenever the data changes: a row’s update time, or a counter. A
Date, a number or a date string works. AnythingDate.parse()can’t read throwsregion_version_invalid. - Only a held region on the page is ordered this way. An inline region has nothing on screen to compare against.
- The third argument is either a schedule function or
{ version }, not both.
import { query, command } from '$app/server';
import { region } from 'ogygia';
import Badge from '$lib/Badge.svelte' with { region: 'raw' };
import { addOne } from '$lib/badge-count';
export const badge = query(async () => region(Badge, {}));
export const bump = command(async () => {
addOne();
return region(Badge, {});
});let count = 0;
export const readCount = () => count;
export const addOne = () => count++;<script>
import { readCount } from '$lib/badge-count';
</script>
<strong>{readCount()}</strong><script>
import { Region } from 'ogygia';
import { badge, bump } from '$lib/badge.remote';
</script>
<Region of={badge()} />
<button onclick={() => bump()}>Add one</button><script>
import BadgePanel from '$lib/BadgePanel.svelte' with {
wake: 'load'
};
</script>
<BadgePanel />import { command } from '$app/server';
import { region } from 'ogygia';
import * as v from 'valibot';
import Card from '$lib/Card.svelte' with { region: 'raw' };
import { db } from '$lib/server/db';
export const rename = command(
v.object({ id: v.string(), name: v.string() }),
async ({ id, name }) => {
const row = await db.rename(id, name);
return region(
Card,
{ id },
{ version: row.updatedAt }
);
}
);Header refresh
A site header is often a lake: static markup with islands and holes inside. When something it’s rendered from changes without a navigation (a saved language, a sign-in), render the header as a held region from a query, and refresh that query from a command. The refreshed header rides the command’s response.
What happens on the refresh:
- The header renders for the page it’s on. The command’s request has no page, but the region’s components still read the calling page’s
page.url,page.route,page.paramsandpage.data. ogygia looks that page up through Kit’s own data request, with the visitor’s cookies, after the command’s change. A server island fetched on its own gets the same treatment. - It morphs in place. The header element stays, and so does every island inside it, with its state. An island that reads
page.datashows the new values. - A hole inside refetches only when its answer can change. When the locale changed, it fetches its new answer and keeps the current one on screen until then.
Keep these in mind:
- Place the region in an island. The command is called in the browser, so the
<Region>and the buttons that call the command go in an island, placed from the layout withwake: 'load'. Acsr = falselayout’s own script never runs in the browser. - Put the region’s remote in its own module. The header’s tree may import remotes, and the remote module imports the header. A component inside the header that triggers the refresh can’t import the command without an import cycle, so dispatch a DOM event and call the command in the island that places the
<Region>. - On a
csr = truepage, Kit ownspage.data. Callawait invalidateAll()first, then refresh the region, so the islands inside read the same values the header rendered with. In dev, ogygia warns when they differ (region_newer_page_data). - Only server loads count. A key from a universal load (
+page.ts,+layout.ts) exists only inside a full page render. In dev, a region that reads one names it (outside_page_data_key). localsset by a load aren’t part of the page. The command’s request never runs the layout’s load, so set what the header reads fromlocalsin the remote function too.
import { command, getRequestEvent, query } from '$app/server';
import { region } from 'ogygia';
import * as v from 'valibot';
import Header from '$lib/Header.svelte';
export const header = query(async () => region(Header, {}));
export const saveLocale = command(
v.string(),
async (locale) => {
getRequestEvent().cookies.set('locale', locale, {
path: '/'
});
await header().refresh();
}
);<script>
import { Region } from 'ogygia';
import { header, saveLocale } from '$lib/header.remote';
</script>
<Region of={header()} />
<button onclick={() => saveLocale('fr')}>Français</button><script>
import HeaderHost from '$lib/HeaderHost.svelte' with {
wake: 'load'
};
let { children } = $props();
</script>
<HeaderHost />
{@render children()}