@ogygia/postrender
Personalization
A stored page is the same for everyone, but a region inside it can still be personal. A server island fetches per visitor in the browser, a live lake refreshes baked HTML, and stitch fills a region into the stored bytes at your server or at an ESI-capable CDN.
A stored page is one copy for every visitor, so a product page can’t show the visitor’s cart count, and a docs page can’t show who’s signed in. You don’t have to give up storing the page for that. Put the personal part in its own region, and the page around it is stored while the region renders per visitor.
ogygia gives you four ways to do it. They differ in where the personal HTML comes from and when the visitor sees it:
| Way | Where it renders | First paint | The stored page |
|---|---|---|---|
| Server island | your server, fetched by the browser | the fallback, then the visitor’s HTML | shared, cached at the CDN |
| Live lake | your server, fetched by the browser | the canonical HTML, then the visitor’s | shared, cached at the CDN |
stitch: 'serve' | your server, on every serve | the visitor’s HTML | per visitor, not cached at the CDN |
stitch: 'edge' | your server, fetched by an ESI CDN | the visitor’s HTML | shared, cached at the CDN |
Use a server island by default, and a live lake when the signed-out version is a fine first paint. When the first paint has to be personal, use stitch: 'edge' if your CDN processes ESI, and stitch: 'serve' if it doesn’t.
<script>
import CartCount from '$lib/CartCount.svelte' with {
render: 'deferred'
};
let { data } = $props();
</script>
<h1>{data.product.name}</h1>
<CartCount>
{#snippet ogygiaFallback()}<span>Cart</span>{/snippet}
</CartCount>Server islands
A server island (render: 'deferred') is a hole in the page’s HTML: the stored page holds its fallback and a signed URL, and the browser fetches the hole’s HTML when the page loads, with the visitor’s cookies. The page above is stored once, and each visitor’s CartCount renders for them.
The hole’s render is its own request, so what it reads never reaches the page’s verdict. CartCount below reads the cart cookie through requestEvent(), which returns the hole’s request, and the product page is still stored. Its top-level await needs Svelte’s async mode; Read the visitor’s request has both.
Two settings matter on a stored page:
- Keep
OGYGIA_SECRETstable. A hole on a stored page is signed for 10 years, so a stored copy never carries an expired URL, but a new build with a new key refuses the holes in copies a CDN still holds. See Keep holes valid across deploys. - List
dataShake(). A hole whose code readspage.datalooks its page up with a data request, which runs the page’s loads, since that lookup never uses the store. WithdataShake(), a hole that reads nopage.dataskips that lookup. It’s whypostrender()requires it.
<script lang="ts">
import { requestEvent } from 'ogygia';
import type { RequestEvent } from '@sveltejs/kit';
import { countCart } from '$lib/cart';
const event = requestEvent<RequestEvent>();
const count = await countCart(event?.cookies.get('cart'));
</script>
<span>Cart ({count})</span>Live lakes
A server island paints its fallback first. When the canonical HTML is a good first paint (a header that shows “Sign in” to everyone until it knows better), use a live lake instead: a lake marked render: 'live' inside an island.
Its HTML is rendered into the page and stored with it. A copy served from the store carries a <meta name="ogygia-stored" content="hit"> tag, and on such a page a live lake refreshes when its island first mounts it, not only when the island mounts it again. It fetches fresh HTML from its signed endpoint, with the visitor’s cookies, and swaps it in. The request that renders and stores the page gets no marker, because its HTML is already fresh.
The lake needs an island around it to mount it. A live lake at the top of a page has nothing to mount it, so it never refreshes, and dev warns (live_region_no_host).
Account renders into the stored page as the canonical, signed-out version, and refreshes for each visitor as soon as the page loads.
<script>
import Account from '$lib/Account.svelte' with {
render: 'live',
wake: 'load'
};
</script>
<header>
<a href="/">Home</a>
<Account />
</header><script>
import Header from '$lib/Header.svelte' with { wake: 'load' };
let { children } = $props();
</script>
<Header />
{@render children()}Server stitching
Sometimes even a moment of the canonical HTML is too much, or the personal part has to be in the HTML itself, for a reader with no JavaScript. Then add stitch: 'serve' to a server island.
On every serve of the stored page, your server renders the hole for this visitor, with their cookies, and splices its HTML into the stored bytes before it responds. The page is still rendered once and stored, and a hit costs one region render instead of a page. The visitor’s HTML is in the first response, in view-source too, and needs no JavaScript.
Since every response is now per visitor, the page is sent with cache-control: private, no-store, with no etag or last-modified (a 304 would skip the splice) and no edge headers. A CDN doesn’t keep it.
The splice fails open. A hole whose render fails, times out or is refused keeps its fallback in the response, and the browser fetches it like any server island. Several holes render at once. Islands inside a stitched hole wake on their own.
<script>
import AccountMenu from '$lib/AccountMenu.svelte' with {
render: 'deferred',
stitch: 'serve'
};
let { data } = $props();
</script>
<AccountMenu>
{#snippet ogygiaFallback()}<a href="/login">Sign in</a
>{/snippet}
</AccountMenu>
<h1>{data.product.name}</h1>Edge stitching (ESI)
stitch: 'serve' gives up the CDN’s copy. A CDN that processes ESI (edge side includes; Akamai, Fastly and Varnish do) can do the splice itself, so you keep both. Mark the hole stitch: 'edge'.
The page stays an ordinary stored page, with public, s-maxage, validators and edge headers. When it’s stored, each edge hole is rewritten into an include of the hole’s own signed URL, and the response carries surrogate-control: content="ESI/1.0" to ask the CDN to process it. On each request the CDN serves the page from its cache, fetches only the include from your server with the visitor’s cookies, and splices it in. Your server renders one region per request, and the page never leaves the edge.
You have to turn ESI processing on for the CDN property, and let it forward the visitor’s cookies on the include’s fetch.
The stored HTML wraps the hole in <esi:remove> and follows it with the include:
It degrades by itself. Where ESI isn’t processed (straight to your server, in dev, or on a CDN without ESI), <esi:remove> is an unknown element whose content still shows and <esi:include> does nothing, so the fallback ships and the browser fetches the hole like any server island. With onerror="continue", a failed include leaves the CDN’s page without it.
<script>
import AccountMenu from '$lib/AccountMenu.svelte' with {
render: 'deferred',
stitch: 'edge'
};
let { data } = $props();
</script>
<AccountMenu>
{#snippet ogygiaFallback()}<a href="/login">Sign in</a
>{/snippet}
</AccountMenu>
<h1>{data.product.name}</h1><esi:remove
><ogygia-region endpoint="…"
>…fallback…</ogygia-region
></esi:remove
><esi:include src="/__ogygia__?…" onerror="continue" />Limits
stitchneedsrender: 'deferred'. On any other import it fails the build (region_import_invalid).- In a preset,
stitch: truemeans'serve'. TheOgygiaPresettype doesn’t liststitchyet, though the build accepts it (#2278). - Stitching happens only on stored pages. In dev, on a route that’s opted out, or on a request postrender doesn’t look up (a query string, a cookie on a page not yet stored), a stitched hole is an ordinary server island: the fallback ships and the browser fetches it.
- Every stitched serve renders. A
servehole costs a region render per request, and anedgehole costs one per CDN request. Keep the stitched part small, and put the rest in server islands.