Caching
Torpor doesn't cache your data between requests: your loads run on every request, so pages never show stale data. Caching data is left to the layer that knows when it changes — your database or ORM, or your own Redis or KV store. What the framework does is the caching only it can do: HTTP caching of the responses it renders, and running a call once per request.
Cache headers
Call event.cache in a load, hook or endpoint to set the response's Cache-Control and Vary headers. Calls from a page's layouts and the page itself are merged, and the most restrictive setting wins: the smallest ages, private or noStore if any call asks for it, and every vary header:
// routes/blog/+page.server.ts
export default {
load: async (event) => {
// CDNs may serve it for 10 minutes, browsers for 1, and either may
// serve it stale for an hour while they fetch a fresh copy
event.cache({ maxAge: 60, sMaxAge: 600, staleWhileRevalidate: 3600 });
return ok({ posts: await getPosts() });
},
} satisfies PageServerEndPoint<"/blog">; The settings apply to successful GET responses: the page, its client data requests and endpoints. A Cache-Control header set directly with event.headers.set takes precedence.
Private by default
A response that read or set a cookie — through event.cookies, event.session or a flash message — depends on who asked for it, so it gets Cache-Control: private, no-cache unless you say otherwise. That stops a CDN from serving one user's page to another. With event.cache, such a response stays private (the CDN-only sMaxAge is dropped) unless you pass private: false, which you should only do when its content doesn't depend on the cookie. In dev, that logs a warning.
ETags
Rendered pages and client data responses get a weak ETag from a hash of their content. When the browser asks again with a matching If-None-Match, the response is a 304 with no body. The loads still run every time, so the data is always current; an unchanged page just isn't sent twice. Data responses are marked no-cache, so the browser keeps them for their ETag but always checks with the server first. (Pages that stream deferred data don't get an ETag, since their content isn't known up front.)
Build assets
The scripts, styles and images the build writes to /assets have a content hash in their names, so a deploy that changes one gives it a new name. They're served with Cache-Control: public, max-age=31536000, immutable, so browsers and CDNs keep them for a year without checking back. The Node server sets the header itself, and the Cloudflare adapter writes it to a _headers file, ahead of any _headers file in src/public. Files from src/public/assets keep their own names, so they don't get the long lifetime.
Running a call once per request
dedupe wraps a function so that it runs once per request for each set of arguments. A layout and its page can both get the current user, and the database is only asked once. Nothing is kept after the request:
// lib/users.ts
import { dedupe } from "@torpor/build/server";
export const getUser = dedupe((event, id: string) => db.users.find(id));
// In any load, hook or action
const user = await getUser(event, event.appData.userId); Arguments are compared by their JSON. A call that throws, or returns a promise that rejects, isn't kept, so a later call in the same request retries.
Background work
If you keep your own cache, you'll often want to refresh it after the response has gone. event.waitUntil keeps a promise running after the response is sent. On Cloudflare it uses the worker's ctx.waitUntil, without which the work would be cut off; on Node the promise just runs on. A rejection is logged rather than left unhandled:
load: async (event) => {
const cached = await kv.get("posts");
if (cached && isStale(cached)) {
// Serve the cached posts now, and refresh them for the next request
event.waitUntil(refreshPosts());
}
return ok({ posts: cached?.posts ?? (await refreshPosts()) });
},