@ogygia/flags
Caching
A page that reads a flag belongs to one visitor, so postrender never stores it and a cached server island never shares it across variants. Prerendered pages and CDN caches need a plan of their own.
On this page
A flag decides per visitor, so any page that reads one is that visitor’s page. Caching it once and serving it to everyone would give every visitor the first visitor’s variant. ogygia’s own caches know this: calling a flag (or its pick or stamp) marks the render as per-visitor, and they skip or key what they keep. Caches outside ogygia, such as a CDN or a prerendered file, don’t, and need you to choose.
import { hero } from '$lib/flags';
import type { PageServerLoad } from './$types';
// this read keeps /pricing out of postrender's store: refusal `flag:hero`
export const load: PageServerLoad = (event) => ({
variant: hero(event)
});Postrender
Postrender stores a page on its first request when its render read nothing personal. A flag read counts as personal, as flag:<name> in its list of reads, so a page under an A/B test is rendered per request and never stored with one visitor’s variant. On the dev server, the refusal is printed once per path (postrender_stays_per_request).
This needs the flags() plugin: without it, nothing reports the read, and a stored page could keep the variant of whoever asked first. The server says flags_plugin_not_running when that happens.
.value(c) doesn’t count as a read today (#2310). A page whose only flag read is a vendor payload can be stored with the first visitor’s payload, so opt it out with config = { postrender: false } and send Cache-Control: private yourself, since an opted-out route gets no cache-control from postrender (#2300). Or call the flag in the same load (Vendor values). The same holds for a cached server island.
Running a source doesn’t count as a read either. The source runs on every request and may read the og-vid cookie, but only a flag read marks the page, so a page that reads no flag still stores.
To keep a page stored and still run an experiment on part of it, move the part that reads the flag into a server island. The stored shell is the same for everyone, and the island renders per visitor in its own request, reading the flag through requestEvent() (Personal parts). Offer.svelte is the component shown there.
<script lang="ts">
import Offer from '$lib/Offer.svelte' with {
render: 'deferred'
};
</script>
<h1>Pricing</h1>
<Offer />Server islands
A server island with a maxAge keeps its render in a server-side cache, keyed by the island, its props and the visitor’s session. When a render reads a flag, it’s kept only if that key names the visitor. For a visitor without a session it isn’t kept at all, so two visitors in different variants never share a render, and a visitor who asks while another bucket’s render is in flight gets their own.
The island’s Cache-Control: private, max-age=… lets only that visitor’s browser keep it, so a browser-cached island stays that visitor’s variant until it expires.
Prerendering
A prerendered page is rendered once, at build time, where there’s no visitor, no cookie and no visitor’s URL. Every flag read there answers what it answers for nobody: the control of a weighted flag and off for a rollout, unless a source decides otherwise. That one render is the page for everyone. Read flags in a server island on such a page, which renders per visitor when the page loads (Prerendering).
CDN caching
Flags don’t set Cache-Control or Vary on the response. If your app or your host caches a page publicly and the page reads a flag, the CDN keeps one variant and serves it to everyone. So set Cache-Control: private yourself on every page that reads a flag. Postrender, when listed, sends private, no-store on most HTML pages it refuses, but not on every response: requests that arrive while the first render of a page is running get none (#2300). Without postrender, or on a route opted out of it, nothing sends it. For pages that read a flag:
- send
Cache-Control: privateorno-storeyourself, as below; - or cache the shell publicly and read the flag in a server island, as above.
Mind the og-vid cookie too. A response that sets it carries Set-Cookie: og-vid=…, and a CDN that caches it hands that one id, and so one bucket, to everyone who gets the copy. Postrender won’t store such a response either, so a first visitor’s request never makes the stored copy. Send any response that sets og-vid with Cache-Control: private.
import { hero } from '$lib/flags';
import type { PageServerLoad } from './$types';
export const load: PageServerLoad = (event) => {
event.setHeaders({ 'cache-control': 'private, no-store' });
return { variant: hero(event) };
};