Serve a kept page stale at the edge for a day while it is read again, the browser keeping the route's own Cache-Control
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by oscabriel · committed by oscabriel · parent 5e2dcf6 · Browse files · 2 files·+80 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+80 added−1 removed
- tests:+52 added−1 removed
- other:+28 added−0 removed
Provenance
- Commit
cdfd323- Tree
1ee0b0d- Author
oscabriel <oscarhernandeziv@gmail.com>- Committer
oscabriel <oscarhernandeziv@gmail.com>- Parent
5e2dcf6
2 files changed+80 added−1 removed
modified:apps/web/src/server/edge-cache.ts+28 added
1234567891011121314151617181914 unmodified lines34353637383940414243444546474849505152535455565724 unmodified lines828384858687888990919293949530 unmodified lines// Workers Cache keys an answer by path and query alone, not by host, `Cookie` or `Authorization`.// An answer to a request with a credential, one that sets a cookie, or one with no `Cache-Control` is kept nowhere;// any other varies by `Cookie`, so a signed-in reader is never handed a page kept from a signed-out one.// A page the edge keeps is served stale for a day while it is read again, so a page seldom visited still answers// at the edge's speed; the browser keeps its own `Cache-Control`, and a page's events bring it up to date once drawn.import { Effect } from "effect";
export const KEPT_NOWHERE = "private, no-store";
const EDGE_OVERRIDES = ["cdn-cache-control", "cloudflare-cdn-cache-control"];
const EDGE_ONLY = "cloudflare-cdn-cache-control";
export const EDGE_STALE_SECONDS = 86_400;
const directivesOf = (cacheControl: string) => cacheControl .split(",") .map((directive) => directive.trim().toLowerCase())14 unmodified lines directivesOf(cacheControl).some( (directive) => directive === "private" || directive === "no-store" );
// Only a found page with an age of its own: a miss may be answered once it is made, and an immutable answer is never stale.const staleLonger = (cacheControl: string, status: number) => { const directives = directivesOf(cacheControl);
return status !== 200 || directives.includes("immutable") || !directives.some((directive) => directive.startsWith("max-age=")) ? undefined : [ ...directives.filter( (directive) => !directive.startsWith("stale-while-revalidate=") ), `stale-while-revalidate=${EDGE_STALE_SECONDS}`, ].join(", ");};
const varyingByCookie = (vary: string | null) => { const names = (vary ?? "") .split(",") .map((name) => name.trim())24 unmodified lines }
if (cacheControl !== null && !keptNowhere(cacheControl)) { headers.set("vary", varyingByCookie(headers.get("vary")));
const atTheEdge = staleLonger(cacheControl, response.status);
if (atTheEdge !== undefined) { headers.set(EDGE_ONLY, atTheEdge); } }
return headers;};30 unmodified linesmodified:apps/web/test/edge-cache.test.ts+52 added-1 removed
8 unmodified lines9 import { forwarded } from "../src/features/agents/forward.ts";10 import { backend } from "../src/server/backend.ts";11 import { branchPage, personalPage } from "../src/server/cache-headers.ts";12 import {13+ EDGE_STALE_SECONDS,14 edgeEntry,15 forTheEdge,16 KEPT_NOWHERE,17 } from "../src/server/edge-cache.ts";144 unmodified lines162 163 expect([164 response.headers.get("cdn-cache-control"),165 response.headers.get("cloudflare-cdn-cache-control"),165- ]).toStrictEqual([null, null]);166+ ]).toStrictEqual([167+ null,168+ `public, max-age=30, stale-while-revalidate=${EDGE_STALE_SECONDS}`,169+ ]);170 });171 172+ it.each([173+ ["a page at main", branchPage, "public, max-age=30"],174+ [175+ "an agent's document",176+ { "cache-control": "public, max-age=60" },177+ "public, max-age=60",178+ ],179+ ] as const)(180+ "serves %s stale at the edge for a day while it reads it again, and tells the browser what the route said",181+ (_, headers, fresh) => {182+ const response = forTheEdge(get("/oscabriel/datekit"), answer(headers));183+184+ expect({185+ browser: response.headers.get("cache-control"),186+ edge: response.headers.get("cloudflare-cdn-cache-control"),187+ }).toStrictEqual({188+ browser: new Headers(headers).get("cache-control"),189+ edge: `${fresh}, stale-while-revalidate=86400`,190+ });191+ }192+ );193+194+ it.each([195+ ["a page about its reader", get("/inbox"), answer(personalPage)],196+ [197+ "a page asked for with a session",198+ get("/oscabriel/datekit", { cookie: SIGNED_IN }),199+ answer(branchPage),200+ ],201+ ["a page not found", get("/nowhere"), answer(branchPage, 404)],202+ [203+ "a raw file at a commit",204+ get("/raw"),205+ answer({ "cache-control": "public, max-age=31536000, immutable" }),206+ ],207+ ["an answer with no age", get("/x"), answer({ "cache-control": "public" })],208+ ] as const)(209+ "gives the edge no longer window for %s",210+ (_, request, response) => {211+ expect(212+ forTheEdge(request, response).headers.get(213+ "cloudflare-cdn-cache-control"214+ )215+ ).toBeNull();216+ }217+ );218+219 it.effect("comes back with its status, its other headers and its body", () =>220 Effect.gen(function* () {221 const response = forTheEdge(222 get("/nowhere"),293 unmodified lines