Skip to content

@ogygia/postrender

Postrender

@ogygia/postrender stores a page's rendered HTML on its first request, when its render read nothing personal, and serves the stored bytes to every later visitor until you clear it. Kit, your loads and Svelte don't run on a hit, and server islands inside the page still render per visitor.

On this page

Many pages would be prerendered if their content didn’t change after the build. A CMS page, a docs page or a product listing gets a new version when an editor publishes, so it renders on every request instead, and almost every one of those renders produces the same HTML.

Prerendering renders a page at build time. Postrender renders it at request time instead. The first request for a URL renders the page as usual. If that render read nothing personal (no cookie, no flag, no visitor’s header), postrender stores the rendered HTML and serves the stored bytes to every later request for that URL. Kit, your loads and Svelte don’t run on a hit. When the content changes, you clear the page, and the next request renders it again, once.

So the cost of a page moves from one render per request to one render per change. Prerendering is the special case where the only change is a deploy.

With the package set up, that endpoint is all you add: every page that can be stored is stored, and a CMS webhook that posts the changed page’s path clears it. A real webhook also checks a shared secret; Invalidation shows one.

import { postrender } from '@ogygia/postrender';
import type { RequestHandler } from './$types';

export const POST: RequestHandler = async ({ request }) => {
  const { path } = (await request.json()) as { path: string };
  await postrender.invalidate(path);
  return new Response(null, { status: 204 });
};

Installation

Install @ogygia/postrender with @ogygia/data-shake, and list both plugins beside ogygia(), before sveltekit(). postrender() needs dataShake(): with it, a server island whose code reads no page.data skips looking up its page, a lookup that would run the page’s loads again on every fetch. Without it, the build fails with plugin_needs_plugin, naming the line to add.

postrender() takes two options. ttl is how many seconds a stored page lives at most (default 86400, one day; see The TTL backstop). default says whether every page may be stored (true, the default) or only the routes that ask (false; see Route config).

pnpm add @ogygia/postrender @ogygia/data-shake
import { sveltekit } from '@sveltejs/kit/vite';
import { ogygia } from 'ogygia/vite';
import { postrender } from '@ogygia/postrender/vite';
import { dataShake } from '@ogygia/data-shake/vite';
import { defineConfig } from 'vite';

export default defineConfig({
  plugins: [
    ogygia(),
  postrender(),
  dataShake(),
    sveltekit()
  ]
});

Server handle

postrender.handle() holds the live parts that can’t go in the Vite config: the store the pages live in and the CDNs to purge. Put it before ogygia.handle() in sequence(), because a stored page is served from inside ogygia’s handle, before anything renders, and the store has to be known by then.

With no arguments, pages go to a memory store inside the server process. For an app that runs on more than one instance, pass a shared store; Stores and CDNs covers each one.

Each setup mistake is named:

  • The handle after ogygia’s, or outside the sequence(): in dev, every page request fails with an error that names the fix. In production the server warns once and still serves from the handle’s store.
  • No handle at all: pages go to the memory store, and the server warns once with the line to paste.
  • The handle without postrender() in the Vite config: nothing is ever stored, and the server warns once (postrender_handle_unlisted).
import { sequence } from '@sveltejs/kit/hooks';
import * as ogygia from 'ogygia/server';
import { postrender } from '@ogygia/postrender';

export const handle = sequence(
  postrender.handle(),
  ogygia.handle()
);

First stored page

Nothing is stored in dev, so your edits always show (see Debugging). Build the app and run it, then request a page twice. The x-ogygia-postrender header says what happened: stored on the request that rendered and stored the page, hit on every request served from the store.

The first response also carries cache-control: public, s-maxage=86400, an etag and a last-modified, so a CDN in front can keep the page too. A page that read something personal gets no x-ogygia-postrender header and cache-control: private, no-store instead, unless your app set one. What gets stored lists every check.

Use a GET, as above, not curl -I (why).

pnpm build && pnpm preview
curl -s -o /dev/null -D - http://localhost:4173/docs/intro | grep -i -e x-ogygia-postrender -e cache-control
# cache-control: public, s-maxage=86400
# x-ogygia-postrender: stored
curl -s -o /dev/null -D - http://localhost:4173/docs/intro | grep -i x-ogygia-postrender
# x-ogygia-postrender: hit

Comparison

All three keep a rendered page so it isn’t rendered again. They differ in when the page is rendered, which pages qualify, and how a copy is cleared.

Prerendering renders at build time. You list every page up front, and a change to any of them needs a new build. Postrender renders a page the first time someone asks for it, so it covers paths nobody knew at build time (every product, every CMS slug), and one page can be cleared and rendered again without a build.

A CDN cache keeps whatever the origin sends, by URL and by the rules you set for the property. It can’t see whether a render read a cookie, so a blanket rule that caches HTML can hand one visitor’s page to everyone, and a cautious rule caches nothing. Postrender watches each render and decides per page: a page that read nothing personal is stored and sent with public, s-maxage, and one that did is sent with private, no-store. It also keeps its own copy at the origin, so a CDN miss is served from the store instead of a render, and it purges the CDN when you clear a page.

Postrender doesn’t replace a CDN. Put one in front and give postrender its purge credentials, and each page is cached at both: the CDN serves it close to the visitor, and postrender answers the CDN’s misses and revalidations without rendering.

PrerenderingCDN cachePostrender
Renderedat buildon a miss, at the originon the first request, then never until cleared
Which pagesthe ones you listthe ones your rules matcheach page whose render read nothing personal
A request with a cookierendered with no visitorcached if your rules say sonever stored; served the shared copy if one exists
Cleared bya new buildthe CDN’s purge API, or TTLinvalidate(), invalidateWhere(), a source, a deploy
Personal partsserver islandsnoneserver islands, live lakes, and stitching at origin or edge

Package contents

PageWhat it covers
Route configThe postrender key in Kit’s config, opting routes out or in, and route tables.
What gets storedWhich requests use the store, the reads that keep a page per-request, the key, cookies, and what a stored page keeps.
PersonalizationServer islands, live lakes, and stitching a region in at your server or at an ESI CDN.
Invalidationinvalidate(), invalidateWhere(), data sources, posts, deploys and the TTL.
Stores and CDNsThe memory, Valkey, Upstash and Workers KV stores, the Akamai, CloudFront and Cloudflare edges, and failures.
Custom adaptersThe PostrenderStore and EdgeAdapter contracts, for a store or CDN that isn’t built in.
DebuggingThe x-ogygia-postrender header, why a page isn’t stored, why a page is stale, and every message.
@ogygia/postrenderThe generated API reference.

Integrations

  • @ogygia/router reads the same postrender key from a route table’s config, at table, layout and page level. See Set page options with config.
  • @ogygia/flags marks every flag read as personal, so a page under an A/B test is never stored with one visitor’s variant. The flags() plugin has to be listed for this.
  • @ogygia/federation carries clears across apps: a publish or a deploy on another team clears every stored page that baked its fragments (cross-app thaw).
  • @ogygia/content pages are usually the same for every visitor, so they store well. A CMS webhook clears the changed entry’s path.
  • @ogygia/devtools: the SSR profiler names a page that renders the same bytes on every request, with no personal reads, as one a cache or postrender could answer (Slow patterns). A browser with devtools switched on never gets a stored page; see Bypass the store.