Skip to content
Myr

edge-cache.ts

This file

Path

apps/web/src/server/edge-cache.ts

97 lines · 2.9 KiB · TypeScript

Last changed by

Merge build/comments-web-rest into mainoscabrielbefore adoption0eb1248

History of this file

Take it

Merge build/comments-web-rest into mainoscabrielbefore adoption0eb1248

lines 1–3why, one step back
1// Workers Cache keys an answer by path and query alone, not by host, `Cookie` or `Authorization`.
2// An answer to a request with a credential, one that sets a cookie, or one with no `Cache-Control` is kept nowhere;3// any other varies by `Cookie`, so a signed-in reader is never handed a page kept from a signed-out one.

Merge perf/workers-cache into mainoscabrielbefore adoptionccd5da2

lines 4–18why, one step back
4import { Effect } from "effect";
56export const KEPT_NOWHERE = "private, no-store";78const EDGE_OVERRIDES = ["cdn-cache-control", "cloudflare-cdn-cache-control"];910const directivesOf = (cacheControl: string) =>11 cacheControl12 .split(",")13 .map((directive) => directive.trim().toLowerCase())14 .filter((directive) => directive !== "");1516const speaksForSomeone = (request: Request) =>17 request.headers.has("cookie") || request.headers.has("authorization");18

Merge build/comments-web-rest into mainoscabrielbefore adoption0eb1248

line 19why, one step back
19// A field-naming directive (`private="set-cookie"`) lets the cache store the answer with that field stripped.

Merge perf/workers-cache into mainoscabrielbefore adoptionccd5da2

lines 20–97why, one step back
20const unsafeFor = (cacheControl: string | null) =>
21 cacheControl === null ||22 directivesOf(cacheControl).some(23 (directive) =>24 directive.startsWith("private=") || directive.startsWith("no-cache=")25 );2627const keptNowhere = (cacheControl: string) =>28 directivesOf(cacheControl).some(29 (directive) => directive === "private" || directive === "no-store"30 );3132const varyingByCookie = (vary: string | null) => {33 const names = (vary ?? "")34 .split(",")35 .map((name) => name.trim())36 .filter((name) => name !== "");3738 return names.some((name) => name.toLowerCase() === "cookie")39 ? names.join(", ")40 : [...names, "Cookie"].join(", ");41};4243const edgeHeaders = (request: Request, response: Response) => {44 const headers = new Headers(response.headers);4546 for (const name of EDGE_OVERRIDES) {47 headers.delete(name);48 }4950 const cacheControl = headers.get("cache-control");5152 if (53 speaksForSomeone(request) ||54 headers.has("set-cookie") ||55 unsafeFor(cacheControl)56 ) {57 headers.set("cache-control", KEPT_NOWHERE);5859 return headers;60 }6162 if (cacheControl !== null && !keptNowhere(cacheControl)) {63 headers.set("vary", varyingByCookie(headers.get("vary")));64 }6566 return headers;67};6869export const forTheEdge = (request: Request, response: Response): Response =>70 new Response(response.body, {71 headers: edgeHeaders(request, response),72 status: response.status,73 statusText: response.statusText,74 });7576interface Entry<Rest extends readonly unknown[]> {77 readonly fetch: (78 request: Request,79 ...rest: Rest80 ) => Promise<Response> | Response;81}8283interface EdgeEntry<Rest extends readonly unknown[]> {84 readonly fetch: (request: Request, ...rest: Rest) => Promise<Response>;85}8687export const edgeEntry = <Rest extends readonly unknown[]>(88 entry: Entry<Rest>89): EdgeEntry<Rest> => ({90 fetch: (request, ...rest) =>91 Effect.runPromise(92 Effect.map(93 Effect.promise(() => Promise.resolve(entry.fetch(request, ...rest))),94 (response) => forTheEdge(request, response)95 )96 ),97});