Regions
Server islands
A hole in the page that the server fills after the page loads. The page shows a fallback first, then swaps in HTML rendered for this request, with no JavaScript shipped for it.
On this page
Some parts of a page depend on the request but don’t need to be interactive: a greeting with the visitor’s name, a “3 unread” count, a panel backed by a slow service. Rendering them in the page’s own pass makes the whole page personal, and as slow as its slowest part.
A server island leaves a hole instead. The page ships with a fallback in that spot, and once the page has loaded, the browser fetches the real HTML from your server and swaps it in. Nothing hydrates, because it’s server HTML arriving late, so it ships no JavaScript. You make one by marking the import with render: 'deferred':
The hole’s HTML comes from the endpoint that ogygia.handle() serves, so you need the server handle. The browser asks for it with a signed URL made when the page rendered, so a visitor can only ask for that component with those props.
The demo below is a real server island. Its time stamp is set when the hole’s HTML is rendered, after the page loaded, not when the page rendered.
To watch the round trip, press ↻ below. The frame loads the same server island again: the hole goes back to its fallback, fetches fresh HTML and swaps it in. The time changes every time, because a server island isn’t cached by default. The server in this demo waits 600ms before it renders, so the fallback stays long enough to see.
<script>
import Greeting from '$lib/Greeting.svelte' with {
render: 'deferred'
};
</script>
<Greeting />Fallbacks
Pass an ogygiaFallback snippet at the call site, and it fills the hole until the HTML arrives. Without one, the hole is empty space, which can shift the layout when the content lands.
The snippet is consumed by the build, so the component never receives it. It still has to be declared on the component, because svelte-check checks your source as written and would otherwise reject a prop the component doesn’t list. The Fallback type from ogygia adds it for you.
ogygiaFallback is the only snippet or child a server island takes. Other markup between its tags fails the build (region_children_not_portable), because the hole renders on its own, from its props alone.
<script lang="ts">
import type { Fallback } from 'ogygia';
let { site }: Fallback<{ site: string }> = $props();
</script>
<p>Welcome back to {site}.</p><script>
import Greeting from '$lib/Greeting.svelte' with {
render: 'deferred'
};
</script>
<Greeting site="the shop">
{#snippet ogygiaFallback()}
<p class="skeleton">Welcome.</p>
{/snippet}
</Greeting>Request data
A server island renders in its own request, the one the browser makes for the hole. It often needs to know about the visitor: the session in locals, a cookie, a header. requestEvent() from ogygia returns Kit’s RequestEvent for that request. It works where Kit’s $app/server can’t: Kit refuses that import in a component that a csr = true page also renders.
Call it during setup, before the first await, the way you’d call getContext. It returns null in the browser. The same call works in a lake, in a layout, and in an island’s server render.
The hole’s request isn’t the page’s request, but page.url, page.route, page.params and page.data still read the page the hole sits on: ogygia looks that page up through Kit’s own data request, with the visitor’s cookies. Only server loads count there. A key from a universal load (+page.ts, +layout.ts) isn’t in it, and dev names the key a hole reads (outside_page_data_key).
The example below reads locals.user, so declare user on App.Locals in src/app.d.ts. It also uses top-level await, which needs Svelte’s async mode: set compilerOptions: { experimental: { async: true } } in svelte.config.js. A new sv create app doesn’t have it.
declare global {
namespace App {
interface Locals {
user?: { id: string; name: string };
}
}
}
export {};<script lang="ts">
import { requestEvent } from 'ogygia';
import type { RequestEvent } from '@sveltejs/kit';
import { getAccount } from '$lib/account';
const event = requestEvent<RequestEvent>();
const user = event?.locals.user ?? null;
const account = await getAccount(user);
</script>
<a href="/account">{account.name}</a>Kept fallbacks
Often the fallback is the common case. An account slot might show a “Sign in” link to everyone who isn’t signed in, so the hole only matters for visitors who are. Rendering the same markup again for every anonymous visitor, and swapping it over itself, is wasted work.
Call keepFallback() from ogygia and the hole ends with no content: the endpoint answers 204 with an empty body, and the browser keeps the fallback the page already rendered, so nothing is swapped and nothing flashes. The region gets data-og-kept. The fallback belongs to the page, so an island inside it is a full island that wakes on its own, and a kept fallback stays interactive.
Call it as soon as you know, before any other work. It throws a signal the server handle catches, so nothing after it runs. A server island nested inside an island renders inline (see nesting), and calling keepFallback() there fails with keep_fallback_inline.
In the example, AccountSlot keeps the layout’s “Sign in” fallback for anonymous visitors and renders the account link only for a signed-in one.
<script lang="ts">
import {
requestEvent,
keepFallback,
type Fallback
} from 'ogygia';
import type { RequestEvent } from '@sveltejs/kit';
let props: Fallback = $props();
const user = requestEvent<RequestEvent>()?.locals.user;
if (!user) keepFallback();
</script>
<a href="/account">{user?.name}</a><script>
import AccountSlot from '$lib/AccountSlot.svelte' with {
render: 'deferred'
};
let { children } = $props();
</script>
<AccountSlot>
{#snippet ogygiaFallback()}<a href="/login">Sign in</a
>{/snippet}
</AccountSlot>
{@render children()}Fetch timing
A server island fetches on its fetch schedule, which uses the same words as an island’s wake: load (the default), idle, visible, interaction or a media query. Timing and nesting defines each one. A hole below the fold with fetch: 'visible' costs no request until the visitor scrolls to it.
fetch is when the HTML arrives. wake is when an island’s JavaScript runs, and a hole has none, so render: 'deferred' with wake fails the build (hole_wake_removed) and names the fetch to write instead. A hole always fetches, so fetch: 'none' fails the build (region_import_invalid).
On a full page load, each hole is its own request. On an SPA navigation, the router fetches the next page’s load holes together, up to 32 in one request, streamed back in whatever order they finish, so many holes don’t make a waterfall. See single-flight navigation.
In the example, the unread count arrives when the browser is idle, and the reviews when the visitor scrolls to them.
<script>
import Unread from '$lib/Unread.svelte' with {
render: 'deferred',
fetch: 'idle'
};
import Reviews from '$lib/Reviews.svelte' with {
render: 'deferred',
fetch: 'visible'
};
</script>
<Unread />
<Reviews />On-demand loading
With fetch: 'interaction', the hole is fetched only when the visitor reaches for it. That suits content that is large and only sometimes needed: the deeper levels of a nav menu, a filter panel, a details drawer. The fallback is what the page shows meanwhile, such as the menu’s first two levels.
The trigger is the first hover, focus, touch or key press anywhere inside the fallback. The HTML usually arrives before the visitor opens anything, and it is morphed into the fallback rather than swapped, so a menu that is already open stays open and focus stays put. Nothing is replayed; the fallback handles the gesture itself.
<script>
import NavMenu from '$lib/NavMenu.svelte';
import FullMenu from '$lib/FullMenu.svelte' with {
render: 'deferred',
fetch: 'interaction'
};
</script>
<nav>
<FullMenu>
{#snippet ogygiaFallback()}<NavMenu levels={2} />{/snippet}
</FullMenu>
</nav>Prefetching
prefetch splits a hole’s fetch from its swap. The HTML is fetched on the prefetch schedule (load, idle, visible or a media query) and kept in memory, while the swap still waits for fetch. An on-demand hole with prefetch: 'idle' already has its HTML when the first hover comes, so the swap costs no round trip.
prefetch works inline or in a preset. It is never 'interaction', since that’s the swap itself, and never the same as the hole’s own fetch; either one fails the build (region_import_invalid). Without it, an interaction hole fetches and swaps in one step on the first hover.
<script>
import NavMenu from '$lib/NavMenu.svelte';
import FullMenu from '$lib/FullMenu.svelte' with {
render: 'deferred',
fetch: 'interaction',
prefetch: 'idle'
};
</script>
<nav>
<FullMenu>
{#snippet ogygiaFallback()}<NavMenu levels={2} />{/snippet}
</FullMenu>
</nav>Interactivity
A server island never ships JavaScript. When part of its HTML has to respond to clicks, put an ordinary wake island inside the server island’s component. The hole delivers the content, and the island inside makes its buttons work, waking on its own schedule after the HTML lands.
This keeps the two jobs apart: render and fetch decide how and when the HTML arrives, and wake decides when JavaScript runs, on a different component.
The reverse doesn’t work the same way. A server island placed inside an island renders inline, as part of that island’s HTML, and isn’t fetched on its own. Dev says so (nested_server_island_inline).
Like AccountMenu above, Cart uses top-level await, so it needs Svelte’s async mode turned on.
<script lang="ts">
import { requestEvent } from 'ogygia';
import type { RequestEvent } from '@sveltejs/kit';
import CartButtons from '$lib/CartButtons.svelte' with {
wake: 'load'
};
import { getCart } from '$lib/cart';
const cart = await getCart(
requestEvent<RequestEvent>()?.cookies.get('cart')
);
</script>
<ul>
{#each cart.items as item}<li>{item.name}</li>{/each}
</ul>
<CartButtons items={cart.items} />Caching
A server island is fresh on every request. Its HTML is sent with Cache-Control: no-store, which is right for a per-request clock or data tied to a cookie.
To let the browser cache a hole, give it a maxAge in a preset, as a duration string ('30s', '5m', '1h') or seconds. With '1h', the hole is sent with Cache-Control: private, max-age=3600. The maxAge is signed into the hole’s URL, so nobody can point a stolen URL at a longer cache. maxAge isn’t allowed inline in with { … } (region_attribute_unknown); it lives in presets only.
import { sveltekit } from '@sveltejs/kit/vite';
import { ogygia } from 'ogygia/vite';
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [
ogygia({
presets: {
pricing: {
render: 'deferred',
fetch: 'load',
maxAge: '1h'
}
}
}),
sveltekit()
]
});<script>
import Pricing from '$lib/Pricing.svelte' with {
preset: 'pricing'
};
</script>
<Pricing />Prerendering
A server island works on a page with export const prerender = true. The build writes the page as a static file with each hole’s fallback and signed URL in it, your CDN serves it, and each hole is rendered per visitor when the page loads. This is called partial prerendering, and the shell needs no server render.
The holes still need a running server with ogygia.handle() to answer them, so a fully static adapter-static build can’t fill them. Set OGYGIA_SECRET for this too. Without a stable secret, the next deploy signs with a new key and refuses the holes on pages built before it. Partial prerendering has the details, and Postrender does the same for pages rendered on their first request.
export const prerender = true;Limits and failures
- Props travel in the URL. A hole’s props are serialized into its signed URL, up to 8192 characters once encoded. Past that the render fails with
hole_props_too_large. Pass an id and look the record up on the server. - Props are readable. They sit base64-encoded in the URL and in the page’s HTML, signed but not encrypted, and a renewable URL replays them. Keep hole props to routing keys (an id, a locale), and read anything private on the server at render time.
- URLs expire. A hole’s URL is valid for
ttlseconds: 3600 by default, held between 60 and 86400. Prerendered and postrendered pages sign for 10 years. A page a load markspublic, s-maxage=Nsigns for at least 2×N seconds, so the URL outlives the shared cache’s copy. A hole not bound to a session, on a page cached by a CDN, renews itself for up tomaxPageAgeseconds after it expires (default 8 days;0turns renewal off). Seettl. - URLs can be bound to a session. With
ogygia({ sessionCookie: 'name' }), each hole’s signature includes that cookie, so a copied URL fails without the visitor’s cookie. It’s off by default. A hole bound to a session never renews. - A failed fetch keeps the fallback. The browser tries three times, half a second and then a second apart. Offline, it waits until the browser is back online. Then the fallback stays, and dev logs
region_fetch_failed. - A refused URL keeps the fallback. A signature that doesn’t check out is answered
403with anx-ogygia-denyreason, logged asregion_answer_denied. The usual cause is two deployments with different secrets. When the reason points at the server rather than the request (a signature or key from another build), the browser asks once more after half a second, since during a rolling deploy the second ask may reach a server of the page’s own build. - A render that throws keeps the fallback. The endpoint answers
500and logs the error on the server only (region_render_failed). - A render has 10 seconds. Past that, it’s abandoned: the endpoint answers
500, the page keeps the fallback, and the server logsregion_render_failed(andregion_render_abandonedthe first time). Give a slow upstream fetch a timeout of its own. - A redirected answer keeps the fallback. When a handle in front of
ogygia.handle()answers the hole’s request itself, say an auth redirect to/loginor a 404 page, the browser refuses the answer, doesn’t retry, and warnsregion_answer_redirected. Let the endpoint path (/__ogygia__by default) through that handle. - The endpoint is rate limited per client address, 60 requests a minute by default. Each hole counts as one request, including each hole in a batch. A request with no client address gets
429. Behind a proxy, tell your adapter which header holds the visitor’s address. Change the limit withogygia({ rateLimit: { max, windowMs } }), or turn it off withrateLimit: false. SeerateLimit. - Context set by the page doesn’t reach it. The hole renders alone, so it doesn’t see the page’s context. Read context in a nested
wakeisland instead. See Context.
When the HTML lands, the region gets data-hydrated and fires a bubbling ogygia:server event. A kept fallback fires the same event.
Do and don’t
- Do give every server island an
ogygiaFallbackthe same size as its content. - Do use server islands where a part is personal. On an SPA navigation, the next page’s
loadholes arrive in one request. On a full page load each hole is its own request, and every hole counts against the rate limit. - Do use
keepFallback()when the fallback is right for most visitors. - Don’t use a server island for interactive UI. That’s a client island. For content that is personal and interactive, nest a
wakeisland inside it. - Don’t pass whole records as props. Pass ids.