Server-side page caching
The server-side page cache is new in @warlock.js/web 5.12. It is a second,
independent opt-in in config.cache — separate from public/maxAge, which
only decide the Cache-Control header a downstream CDN/proxy should honour.
serverCache has the framework itself hold the resolved bytes and serve a
HIT without re-running loaders or rendering at all.
Opt in with serverCache and tags
Section titled “Opt in with serverCache and tags”import type { PageConfig } from "@warlock.js/web";
export const config: PageConfig = { route: { path: "/products", name: "products.index" }, cache: { public: true, maxAge: 60, serverCache: true, tags: ["products"], // or: tags: (data) => [`product:${data.id}`], ttl: 300, // optional — defaults to maxAge },};serverCacherequirespublic: true— like everyconfig.cacheopt-in — andtags. A stored entry with no tags could never be invalidated early, soserverCache: truewith notagsis a boot-timeInvalidPageCacheOptInError.tagsis a static array, or a function(data, { shared }) => string[]— called right before the entry is stored. The second argument is the sealedsharedsnapshot at store time, so a route can tag by something middleware resolved (a tenant, a theme):tags: (data, { shared }) => [`theme:${shared.theme}`].ttlis the server cache’s own freshness window in seconds, independent of the CDN-facingmaxAge. Omit it to reusemaxAge.varyBy?: (request) => stringadds a request-derived component to the cache key, for entries that split on something other than the host (a header an edge proxy sets, for example). It runs before the cache lookup — and before middleware — so it must read the request directly, e.g.varyBy: (request) => String(request.header("x-theme") ?? ""). Don’t vary on a cookie: a request that carries one bypasses the cache entirely (see below). A non-functionvaryByis a boot-time error naming the page file.query?: string[]lists the query parameters that change the page. Only these enter the cache key, so?page=2&ref=xand?page=2&ref=yshare one entry. Omit it and every parameter except common tracking ones (utm_*,fbclid,gclid,msclkid,mc_eid,_ga) enters the key.varyHeaders?: string[]lists request headers the page depends on. They are added to the response’sVaryheader so a CDN keeps one copy per value, for example the headervaryByreads:varyHeaders: ["x-theme"].
export const config: PageConfig = { route: { path: "/posts", name: "posts.index" }, cache: { public: true, maxAge: 60, serverCache: true, tags: ["posts"], query: ["page", "sort"], // ?page=2&sort=new; any other parameter is ignored by the key },};What gets cached
Section titled “What gets cached”Storage is checked once per render and requires all of:
GETonly — a mutating method is never cached.status === 200.- No
Set-Cookieon the response. - A provably unauthenticated request — the same fail-closed rule that governs
Cache-Control. If Warlock cannot determine whether the request used authenticated state, it refuses to store, exactly as it refuses to emit a publicCache-Control. - No query parameter that the key leaves out (a tracking parameter, or one
outside
cache.query). The page and its loaders still see every parameter, so such a render could show a value no other visitor asked for. It is served, and may itself be served a stored entry, but it is never stored. Tagged traffic (?utm_source=...) gets HITs once a clean request has stored the entry.
Each route stores at most pageCache.maxEntriesPerRoute variants
(default 1_000). Every distinct query string is its own entry, so without a
ceiling a client looping over ?n=1, ?n=2, … could fill the cache backend.
Past the ceiling, a new variant is rendered and sent normally but not stored,
and one [warlock:web] warning is logged per route. Variants already stored
keep being refreshed, and a slot frees up when its entry’s TTL ends. The count
is kept per process. Declare cache.query so unrelated parameters stop
creating variants, raise the number, or set false to remove the ceiling:
export default { maxEntriesPerRoute: 5_000, // or false};One entry is capped at pageCache.maxEntryBytes (default 1_048_576,
1 MiB). A cache miss copies the document for storage up to that size. A page
larger than the cap is still sent to the visitor in full, but it isn’t cached,
and a [warlock:web] warning is logged. A value that isn’t a positive integer
fails when the config is read.
Bypass happens earlier, before storage is even considered. A request
carrying an Authorization header or any cookie never consults the cache
and is never stored. That check runs before any loader, so a credentialed
visitor is never handed a stale guest page. Their own page is never stored
for guests either, whatever the session cookie is called. The one exception
is the locale cookie: the resolved locale is already part of the key, so a
request whose only cookie is locale is still cached. An empty or malformed
Cookie header counts as a cookie.
In practice, a browser that holds any other cookie (a session, analytics, a consent banner) always gets a fresh render. Anonymous traffic without cookies (first visits, crawlers, link previews) is what the page cache serves.
The cache key
Section titled “The cache key”(host) + (normalised path) + "?" + (sorted query) + "|" + locale + "|" + variant + "|vary=" + varyBy- The key always includes the request
Host(lower-cased). Before 5.13 the host was not part of the key, so two hosts (tenants) serving the same URL shared one entry — a host-routed multi-tenant app could serve tenant B the document andsharedpayload the cache stored for tenant A. Existing entries are simply re-keyed;tagsstill invalidate them. - The path is normalised: trailing slash stripped (except the root
/). The path keeps its case, so/Aboutand/aboutare separate entries. (Changed in 5.21: earlier versions lower-cased the path.) - In multi-site mode the host is the one site selection chose (with the
request’s port). Behind a trusted proxy (
http.trustProxy) that is the forwarded host, the same host every URL in the render is built from, so a render made for a forwarded host is never stored under another host’s key. - The query string is re-serialized with its keys sorted, so
?a=1&b=2and?b=2&a=1resolve to the same entry. Nested values (?filter[status]=draft) are written out in full, with their own keys sorted, so different filters never share an entry. localeis the resolved locale for the request.variantishtmlfor a document request orjsonfor thex-warlock-datarepresentation. A request for the NDJSON representation on aserverCacheroute never streams: the values are fully resolved and stored/served as thejsonvariant instead, since there is nothing left to defer."|vary=" + varyByis appended only when the route declarescache.varyBy(request)(see above).
Changed in 5.21
Section titled “Changed in 5.21”- With CSP enabled, the
htmlvariant is not cached: a cached document would replay a stale nonce. Thejsonvariant is still cached. - A cache-backend outage (redis down, for example) is treated as a miss: the page renders uncached and a throttled warning is logged. It never returns a 500. Store writes are fire-and-forget.
Where entries live: the page-cache namespace
Section titled “Where entries live: the page-cache namespace”Stored pages and their tag index live in the page cache’s own namespace,
warlock.page.<deployment>. It is separate from your app’s cache
globalPrefix, so the GET that stores a page and the POST (or background
job) that invalidates it always see the same entries, even if your
globalPrefix changes from request to request.
The <deployment> part is chosen once, in this order:
pageCache.namespace, when you set it;- otherwise your cache driver’s
globalPrefix, when it is a static string; - otherwise nothing.
Case 3 is fine for an in-process driver (memory, LRU, memory-extended). On a shared backend (redis, pg/database, file) it is a startup error the first time the page cache is used. Two deployments sharing one Redis, such as staging and production, would otherwise read and invalidate each other’s pages. The app name and the Host are not enough to tell them apart. Set a value unique to each deployment:
import { env } from "@warlock.js/core";
export default { namespace: env("PAGE_CACHE_NAMESPACE"), // e.g. "shop-production", "shop-staging"};What gets stored, for a page with a lazy boundary
Section titled “What gets stored, for a page with a lazy boundary”On an eligible HTML MISS, Warlock stores the same streamed document bytes
the MISS visitor received — including any React.lazy boundary that
resolved during that render (a per-request theme, for example). A later HIT
replays exactly that render, never a not-yet-resolved Suspense fallback.
Pages that use defer()
Section titled “Pages that use defer()”A page using defer() is
stored fully resolved — every deferred value is awaited before the entry is
written, exactly as a detected crawler’s render is. A HIT is then served
buffered, not streamed.
Invalidating stored entries
Section titled “Invalidating stored entries”import { invalidatePageCache } from "@warlock.js/web/page-cache";
await invalidatePageCache(["products"]);Imported from @warlock.js/web/page-cache, not the root package: it is
server-only (it reaches @warlock.js/cache), and keeping it off the root
barrel keeps @warlock.js/cache out of the client bundle.
Evicts every stored entry under any of the given tags, through
@warlock.js/cache’s tag index. After eviction it also notifies connected
live pages through core’s socket
connector. Declare config.live.tags to opt an open page into loader re-runs;
config.cache.tags alone only labels its stored response.
Revalidation and compressed entries
Section titled “Revalidation and compressed entries”A HIT carries a weak ETag computed from the stored body. A request whose If-None-Match names it is answered 304 with no body, whichever Accept-Encoding it sent. With http.compression on, an entry is compressed once when it is stored (gzip, and brotli unless http.compression.brotli is false; bodies under the compression threshold stay plain) and a HIT sends the stored form that the request’s Accept-Encoding accepts, so a HIT never compresses again. The Vary header then includes Accept-Encoding. Entries written by an earlier version have no stored forms; they are served as before until they expire.
A CDN keyed on Host
Section titled “A CDN keyed on Host”The cache key uses the host that site selection chose. In a multi-site app, a CDN or shared proxy in front of it must key its own cache on the same host the origin selects from. If the edge keys on the Host header but forwards a client-supplied X-Forwarded-Host to the origin (and http.trustProxy trusts it), one site’s page can be stored by the edge under another site’s URL. Close it at the edge: overwrite X-Forwarded-Host with the matched Host (or strip it from client requests), and include the host in the cache key. The origin cannot close this: it cannot see what the edge keyed on.
The x-warlock-cache header
Section titled “The x-warlock-cache header”Every response from a serverCache route carries x-warlock-cache: hit,
miss, or bypass, alongside the usual Cache-Control.
The Vary header
Section titled “The Vary header”A route with config.cache is Cache-Control: public, so a CDN or shared
proxy may hold it. Its Vary header (the same on a HIT and a MISS) lists what
the response depends on besides the URL:
x-warlock-data, which separates the HTML document from the client navigation data;Cookieandlocale, because the locale cookies and thelocalerequest header select the page’s language. They are left out when the URL pinned the locale (a locale-prefixed or[locale]route) and when the app supports a single locale;User-Agentfor a page that usesdefer();- the route’s
cache.varyHeaders.
A failure page for the same route (a module-load failure, a refusal rendered by
the page host) is no-store but carries the same Vary value as the page, so
the header does not change shape when a page fails.
@warlock.js/cache is an optional peer
Section titled “@warlock.js/cache is an optional peer”@warlock.js/cache is only loaded, via a lazy await import(...), the first
time some route with serverCache: true actually needs the store — an
application that never opts in is never forced to install it. Enabling
serverCache without the package installed fails loudly, naming the package:
A page declares `config.cache.serverCache: true`, but "@warlock.js/cache" could not beloaded. Install it as a dependency of your application — it is an optional peer of@warlock.js/web and is only required when a route opts into the server-side page cache.Cluster behavior
Section titled “Cluster behavior”Cross-worker reach depends on the configured @warlock.js/cache driver:
- A shared driver (redis, pg) removes the entry for every worker on their
next read —
invalidatePageCachereaches the whole cluster. - The memory driver (and other in-process drivers such as LRU or
memory-extended) only clears the calling worker’s own heap. Every other
worker keeps serving its stale entry until
ttl/maxAgeexpires. EnablingserverCachewith an in-process driver in a clustered deployment logs a one-time startup warning; use a shared driver for cluster-wide invalidation.
Push delivery to clients on every worker additionally requires a shared
socket.adapter. A shared cache alone evicts entries but does not broadcast
socket events across processes. Queue/CLI publishers must boot the socket
connector with that adapter too. See
live page deployment requirements.
See also
Section titled “See also”- Streaming SSR and defer() —
the deferred-value contract a
serverCacheroute resolves fully before storing. - Crawler mode — the other path that stores/serves a fully resolved render.
- Multi-theme —
varyByand tag-based invalidation for a per-tenant/per-theme cache.