Skip to content

@ogygia/postrender

Route config

By default every page that can be stored is stored, and a route opts out with Kit's own config page option. Turn the default off and only the routes and layouts that opt in are stored.

On this page

Postrender reads one key, postrender, from Kit’s config page option, the same export const config an adapter reads its own settings from. Its value says whether a route may be stored. Where no route sets it, the default option of postrender() decides, and that is true: every page may be stored.

“May be stored” is all a route can say. A route that’s on is only eligible, and postrender still watches each render and refuses one that read something personal. What gets stored has those checks.

That check alone doesn’t protect a page that changes with who is signed in. Opt every such page out, as the next section shows.

Opting out

Set postrender: false in the route’s config. The route renders on every request, and postrender doesn’t watch it or set any of its headers.

Do this for every page whose content depends on who is signed in: an account page, a dashboard, a cart. Reading the session cookie isn’t enough to keep such a page out of the store. A request with no session cookie (a crawler, a signed-out visitor, a CDN’s own fetch) reads it as empty, which counts as nothing personal, so that render of the signed-out page is stored. From then on every visitor is served that copy, signed-in ones included, because a stored copy is served before any load runs. A request with a cookie never bypasses a stored page.

The only other safe shape is a page that never answers a signed-out visitor with a 200: redirect them with 303 or 307, or answer 401. Those responses are never stored, and a signed-in visitor’s render isn’t stored either, since it carries a cookie. Never use 301 or 308 here: a permanent redirect is stored, and every signed-in visitor would be sent to the sign-in page until you clear it. Opting out says the same thing plainly, so prefer it.

Opt out every csr = true route too, for now: a stored csr = true page breaks client-side navigation to it (Which requests use the store).

An opted-out route gets no cache-control from postrender at all, not even private, no-store (#2300). If a CDN sits in front, set it yourself on every personal page, as the example does, or a CDN that caches responses with no cache-control may keep it.

The example reads the visitor from locals.user, which your auth handle sets from the session (declare it on App.Locals), and passes on only what the page shows. Never put the session cookie itself into page data: it ends up in the HTML.

config works in +page.ts, +page.server.ts, +layout.ts and +layout.server.ts, the files Kit reads it from. Kit merges it from the root layout down to the page, top-level keys only, and the closest one wins. So a layout opts out its whole subtree, and a page below can opt back in:

import type { PageServerLoad } from './$types';

export const config = { postrender: false };

export const load: PageServerLoad = ({
  locals,
  setHeaders
}) => {
  setHeaders({
    'cache-control': 'private, no-store'
  });
  return { name: locals.user?.name ?? null };
};
export const config = { postrender: false };
export const config = { postrender: true };

Opting in

To make storing opt-in, pass default: false. Then nothing is stored unless a page, or a layout above it, sets postrender: true. This suits an app where most pages are personal and only a marketing section or a docs tree is the same for everyone, and it’s the safer start for an app with sign-in, since a page you forget is never stored.

The whole (marketing) group may be stored, except /preview, which renders on every request.

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({ default: false }),
    dataShake(),
    sveltekit()
  ]
});
export const config = { postrender: true };
export const config = { postrender: false };

Requests with cookies

The third value, 'with-credentials', is the same as true but also stores a render made for a request that carries a cookie or an authorization header. Without it, such a request is served a stored copy when there is one, but its own render is never stored. Use it on a route whose pages are the same with or without those headers, on a site that sets an analytics cookie on every visitor, say. Requests with cookies explains why the default is careful here.

export const config = { postrender: 'with-credentials' };

Route tables

A table from @ogygia/router takes the same key in its own config, on routes(), layout() and page(), and merges it the same way: the table, then each layout, then the page. Set page options with config shows it.

A router page is normally sent with cache-control: no-store. On the request whose render may be stored, the router leaves that header off, and postrender sets it instead: public, s-maxage when the page is stored, private, no-store when it isn’t. A request with a cookie for a page not stored yet keeps the router’s no-store. Hits never reach the router.

Literal values

The build reads config from the file, so write the value itself: true, false or 'with-credentials'. A variable, a call or any other expression fails the build with kit_config_not_literal.

Any other literal, such as 'yes', 1 or null, is ignored. The server warns that config.postrender must be true, false or 'with-credentials', and the default option applies to that route.

Write the object itself too. The build reads postrender only from an object literal in the route’s own file, so a config imported from another module, or a postrender key that arrives through a spread ({ ...shared }), isn’t seen, and the default option applies without a warning. The same goes for a +server.ts endpoint: its config isn’t read, and the default decides. When a folder has both a +page.ts and a +page.server.ts that set the key, the server file’s value wins.

const store = true;

export const config = { postrender: store }; // fails the build: write `true` here