Public: anyone can read this; the crew can change it.
/ and /explore answer a held copy with 304 before rendering #14412
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/14411-and-explore-answer-a-held-copy-with-304 → main 8 commits fork 2 threads · 2 unresolved
6 files
changed +269
−49
docs/web.md +1 −1
| @@ -16,7 +16,7 @@ Markup only, and small. It names its assets; it never carries them. Budget: **ho | ||
| 16 | 16 | |
| 17 | 17 | - Assets: immutable, a year. |
| 18 | 18 | - HTML, signed in or carrying any identity (cookie, `Authorization`, edge login headers) or a flash: `no-cache`, no ETag, never shared (`http/edge_cache.ts`, `http/respond.ts`). |
| 19 | - HTML, signed out, on a page that is the same for everyone (home, explore, the front page of a repository that exists and is public; any other path, two segments or not, is `no-cache`): `public, max-age=0, s-maxage=60, stale-while-revalidate=300` and a weak ETag; a revisit costs a 304. The shared copy carries `Vary: Cookie, Authorization, X-Plat-User, X-Plat-Edge-Token`, so an edge keys it on identity: a visitor with any of those misses it and reaches the origin, which answers `no-cache` and never the anonymous page. There is no purge call (the edge's credentials are not the forge's to hold), so staleness is bounded by those 60 + 300 seconds. | |
| 19 | - HTML, signed out, on a page that is the same for everyone (home, explore, the front page of a repository that exists and is public; any other path, two segments or not, is `no-cache`): `public, max-age=0, s-maxage=60, stale-while-revalidate=300` and a weak ETag; a revisit costs a 304 (home and explore name the tag from their inputs and answer a held copy before drawing: the reads still run, so the saving is render time only, `web/page_tag.ts`). The shared copy carries `Vary: Cookie, Authorization, X-Plat-User, X-Plat-Edge-Token`, so an edge keys it on identity: a visitor with any of those misses it and reaches the origin, which answers `no-cache` and never the anonymous page. There is no purge call (the edge's credentials are not the forge's to hold), so staleness is bounded by those 60 + 300 seconds. | |
| 20 | 20 | |
| 21 | 21 | ## The script policy |
| 22 | 22 |
src/web/layout.ts +58 −38
| @@ -56,7 +56,7 @@ import { | ||
| 56 | 56 | spriteAsset, |
| 57 | 57 | stylesheetAsset, |
| 58 | 58 | } from "./asset.ts"; |
| 59 | import { linkHeader, pageLinks, unindexed } from "./standards.ts"; | |
| 59 | import { linkHeader, pageLinks, unindexed, type PageLink } from "./standards.ts"; | |
| 60 | 60 | import { DESCRIPTION_MAX } from "./describe.ts"; |
| 61 | 61 | import { breadcrumbStructuredData } from "./structured_data.ts"; |
| 62 | 62 | import { |
| @@ -100,6 +100,11 @@ export interface PaletteItem { | ||
| 100 | 100 | export interface PageOptions { |
| 101 | 101 | /** The request: user, theme and flash all come from here. */ |
| 102 | 102 | ctx: Ctx; |
| 103 | /** | |
| 104 | * The tag the handler computed from the page's inputs (`page_tag.ts`); absent, the tag is the | |
| 105 | * hash of the bytes. | |
| 106 | */ | |
| 107 | etag?: string; | |
| 103 | 108 | /** |
| 104 | 109 | * A flash for this response itself, for a form that re-renders instead of |
| 105 | 110 | * redirecting. It stands in for the cookie's, so a page never shows two. |
| @@ -860,6 +865,56 @@ export function tabTitle(title: string): string { | ||
| 860 | 865 | return `${own} · forge`; |
| 861 | 866 | } |
| 862 | 867 | |
| 868 | /** | |
| 869 | * The headers a signed-out page carries: the policy naming its hashed assets, the preloads, and | |
| 870 | * the cache line. `page()` writes them on a body, and `page_tag.ts` on the 304 of a held copy, so | |
| 871 | * the two cannot drift. | |
| 872 | */ | |
| 873 | /** What `documentHeaders` reads of a page: the request and the sources its policy must name. */ | |
| 874 | export type DocumentHeadersOptions = Pick<PageOptions, "ctx" | "scriptJs" | "styleCss" | "wasm" | "turnstile" | "objectSrc" | "fontSrc" | "frameSrc" | "frameSelf">; | |
| 875 | ||
| 876 | export function documentHeaders(options: DocumentHeadersOptions, tagged = true, links: readonly PageLink[] = pageLinks(options.ctx)): Headers { | |
| 877 | const { ctx } = options; | |
| 878 | const own = options.scriptJs ? pageScriptAsset(options.scriptJs, ctx.app.config.dataDir) : undefined; | |
| 879 | const styleSources = [bootCss, ...(options.styleCss ? [options.styleCss] : [])] | |
| 880 | .map((css) => `'sha256-${new Bun.CryptoHasher("sha256").update(css).digest("base64")}'`) | |
| 881 | .join(" "); | |
| 882 | const cacheControl = tagged | |
| 883 | ? sharedCache(ctx.request, ctx.url.pathname, (o, n) => getRepo(ctx.app.db, o, n)?.visibility === "public") | |
| 884 | : "no-cache"; | |
| 885 | const headers = new Headers({ | |
| 886 | "content-type": "text/html; charset=utf-8", | |
| 887 | // The hashed stylesheet and scripts are 'self'; the two inline <style>s | |
| 888 | // are named by hash. connect-src 'self' lets the live event stream | |
| 889 | // (EventSource) and fragment fetches reach same-origin. | |
| 890 | "content-security-policy": `default-src 'none'; style-src 'self' ${styleSources}; script-src 'self'${options.wasm ? " 'wasm-unsafe-eval'" : ""}${options.turnstile ? ` ${TURNSTILE_ORIGIN}` : ""}; connect-src 'self'${options.turnstile ? ` ${TURNSTILE_ORIGIN}` : ""}; ${frameSources(options)}worker-src 'self'; manifest-src 'self'; img-src 'self' data:; ${options.objectSrc ? "object-src 'self'; " : ""}${options.fontSrc ? "font-src 'self'; " : ""}form-action 'self'; base-uri 'none'; frame-ancestors 'none'; ${CSP_REPORT_DIRECTIVES}`, | |
| 891 | // Where the browser says what that policy blocked (http/csp_report.ts). | |
| 892 | "reporting-endpoints": CSP_REPORTING_ENDPOINTS, | |
| 893 | "referrer-policy": "same-origin", | |
| 894 | "x-content-type-options": "nosniff", | |
| 895 | // Markup is revalidated every time; only the hashed assets it names are | |
| 896 | // cached, so no cache can pair new HTML with an old stylesheet. A | |
| 897 | // signed-out public page may also sit at the edge for a minute | |
| 898 | // (`sharedCache`); a signed-in one never does. | |
| 899 | "cache-control": cacheControl, | |
| 900 | // The assets the head names, as preloads: an edge turns them into 103 | |
| 901 | // Early Hints, so the browser fetches them before the body arrives. | |
| 902 | link: [ | |
| 903 | linkHeader(links), | |
| 904 | `<${stylesheetAsset().path}>; rel=preload; as=style`, | |
| 905 | `<${shellAsset().path}>; rel=preload; as=script`, | |
| 906 | ...(own ? [`<${own.path}>; rel=preload; as=script`] : []), | |
| 907 | ] | |
| 908 | .filter((l) => l !== "") | |
| 909 | .join(", "), | |
| 910 | "speculation-rules": `"${speculationAsset().path}"`, | |
| 911 | }); | |
| 912 | // A shared copy is keyed on identity, so a request that carries any is a | |
| 913 | // miss at the edge and never sees the anonymous page (docs/web.md). | |
| 914 | if (cacheControl.startsWith("public")) headers.set("vary", EDGE_VARY); | |
| 915 | return headers; | |
| 916 | } | |
| 917 | ||
| 863 | 918 | export function page(options: PageOptions, status = 200): Response { |
| 864 | 919 | // No nonce: a script is a hashed file the policy admits as 'self', and an |
| 865 | 920 | // inline <style> is admitted by the hash of its own text, so the same page |
| @@ -1003,46 +1058,11 @@ export function page(options: PageOptions, status = 200): Response { | ||
| 1003 | 1058 | // costs a 304 (`http/etag.ts`). |
| 1004 | 1059 | const tagged = taggable(ctx.request, ctx.auth.user !== undefined, flash !== undefined) && status === 200; |
| 1005 | 1060 | const text = pointAtSprite(options.tight === true && !/<(pre|textarea)\b/i.test(doc.text) ? doc.text.replace(/[ \t]*\n\s*/g, "\n") : doc.text, spriteAsset().path); |
| 1006 | const styleSources = [bootCss, ...(options.styleCss ? [options.styleCss] : [])] | |
| 1007 | .map((css) => `'sha256-${new Bun.CryptoHasher("sha256").update(css).digest("base64")}'`) | |
| 1008 | .join(" "); | |
| 1009 | const cacheControl = tagged | |
| 1010 | ? sharedCache(ctx.request, ctx.url.pathname, (o, n) => getRepo(ctx.app.db, o, n)?.visibility === "public") | |
| 1011 | : "no-cache"; | |
| 1012 | const headers = new Headers({ | |
| 1013 | "content-type": "text/html; charset=utf-8", | |
| 1014 | // The hashed stylesheet and scripts are 'self'; the two inline <style>s | |
| 1015 | // are named by hash. connect-src 'self' lets the live event stream | |
| 1016 | // (EventSource) and fragment fetches reach same-origin. | |
| 1017 | "content-security-policy": `default-src 'none'; style-src 'self' ${styleSources}; script-src 'self'${options.wasm ? " 'wasm-unsafe-eval'" : ""}${options.turnstile ? ` ${TURNSTILE_ORIGIN}` : ""}; connect-src 'self'${options.turnstile ? ` ${TURNSTILE_ORIGIN}` : ""}; ${frameSources(options)}worker-src 'self'; manifest-src 'self'; img-src 'self' data:; ${options.objectSrc ? "object-src 'self'; " : ""}${options.fontSrc ? "font-src 'self'; " : ""}form-action 'self'; base-uri 'none'; frame-ancestors 'none'; ${CSP_REPORT_DIRECTIVES}`, | |
| 1018 | // Where the browser says what that policy blocked (http/csp_report.ts). | |
| 1019 | "reporting-endpoints": CSP_REPORTING_ENDPOINTS, | |
| 1020 | "referrer-policy": "same-origin", | |
| 1021 | "x-content-type-options": "nosniff", | |
| 1022 | // Markup is revalidated every time; only the hashed assets it names are | |
| 1023 | // cached, so no cache can pair new HTML with an old stylesheet. A | |
| 1024 | // signed-out public page may also sit at the edge for a minute | |
| 1025 | // (`sharedCache`); a signed-in one never does. | |
| 1026 | "cache-control": cacheControl, | |
| 1027 | // The assets the head names, as preloads: an edge turns them into 103 | |
| 1028 | // Early Hints, so the browser fetches them before the body arrives. | |
| 1029 | link: [ | |
| 1030 | linkHeader(links), | |
| 1031 | `<${stylesheetAsset().path}>; rel=preload; as=style`, | |
| 1032 | `<${shellAsset().path}>; rel=preload; as=script`, | |
| 1033 | ...(own ? [`<${own.path}>; rel=preload; as=script`] : []), | |
| 1034 | ] | |
| 1035 | .filter((l) => l !== "") | |
| 1036 | .join(", "), | |
| 1037 | "speculation-rules": `"${speculationAsset().path}"`, | |
| 1038 | }); | |
| 1061 | const headers = documentHeaders(options, tagged, links); | |
| 1039 | 1062 | // A flash is shown exactly once: rendering it is also what clears it. |
| 1040 | 1063 | if (cookieFlash) headers.append("set-cookie", clearFlashCookie(ctx.url, ctx.app.config.url)); |
| 1041 | // A shared copy is keyed on identity, so a request that carries any is a | |
| 1042 | // miss at the edge and never sees the anonymous page (docs/web.md). | |
| 1043 | if (cacheControl.startsWith("public")) headers.set("vary", EDGE_VARY); | |
| 1044 | 1064 | if (tagged) { |
| 1045 | const etag = weakEtag(text); | |
| 1065 | const etag = options.etag ?? weakEtag(text); | |
| 1046 | 1066 | const lastModified = pageStamp(ctx.app, `${ctx.url.pathname}${ctx.url.search}`, etag); |
| 1047 | 1067 | headers.set("etag", etag); |
| 1048 | 1068 | headers.set("last-modified", lastModified); |
src/web/page_tag.ts added +64
| @@ -0,0 +1,64 @@ | ||
| 1 | /** | |
| 2 | * A signed-out page's tag from what it would say, before any of it is drawn (#14411): the handler | |
| 3 | * reads its inputs (rows, settings, who asks), names them here, and a held copy is answered 304 | |
| 4 | * without `page()` building the document. `page()` then stamps the same tag on a miss | |
| 5 | * (`PageOptions.etag`), so the tag a client holds is the one this checks. | |
| 6 | * | |
| 7 | * The page's chrome is part of the inputs: the assets it names, the theme, the address, the | |
| 8 | * sign-in and sign-up doors and the apps the palette lists with their state. The tag also holds | |
| 9 | * this process's boot, so a new build never answers 304 to markup an older one drew. | |
| 10 | */ | |
| 11 | ||
| 12 | import { doorRepos } from "../actions/front.ts"; | |
| 13 | import { appsRuntime } from "../apps/runtime.ts"; | |
| 14 | import { appsForRepos } from "../core/apps.ts"; | |
| 15 | import type { Ctx } from "../http/context.ts"; | |
| 16 | import { taggable, unchanged } from "../http/etag.ts"; | |
| 17 | import { pageStamp } from "../http/page_stamp.ts"; | |
| 18 | import { securityFloor } from "../http/respond.ts"; | |
| 19 | import { beaconAsset, shellAsset, spriteAsset, stylesheetAsset } from "./asset.ts"; | |
| 20 | import { readFlash } from "./flash.ts"; | |
| 21 | import { documentHeaders, signInHref, signUpHref } from "./layout.ts"; | |
| 22 | ||
| 23 | const BOOT = crypto.randomUUID(); | |
| 24 | ||
| 25 | /** What a signed-out visitor's palette lists: the door's apps and whether each is running. */ | |
| 26 | function paletteStamp(ctx: Ctx): unknown { | |
| 27 | const repos = doorRepos(ctx.app).slice(0, 30); | |
| 28 | const apps = appsForRepos(ctx.app.db, repos.map((r) => r.id)); | |
| 29 | const open = repos.flatMap((r) => { | |
| 30 | const app = apps.get(r.id); | |
| 31 | return app === undefined ? [] : [app]; | |
| 32 | }); | |
| 33 | const states = open.length > 0 ? appsRuntime(ctx.app)?.statusMany(open) : undefined; | |
| 34 | return repos.map((r) => { | |
| 35 | const app = apps.get(r.id); | |
| 36 | return [r.ownerName, r.name, r.ownerId, app?.id ?? null, app?.access ?? null, app === undefined ? null : (states?.get(app.id)?.find((e) => e.env === "production")?.state ?? null)]; | |
| 37 | }); | |
| 38 | } | |
| 39 | ||
| 40 | /** | |
| 41 | * The weak tag of this page for this asker, or undefined when it is not one a tag may stand for | |
| 42 | * (signed in, a flash, not a GET). `inputs` is what the page's body would be drawn from; `script` | |
| 43 | * is the page's own script, when it has one. | |
| 44 | */ | |
| 45 | export function pageTag(ctx: Ctx, inputs: unknown, script = ""): string | undefined { | |
| 46 | if (!taggable(ctx.request, ctx.auth.user !== undefined, readFlash(ctx.request) !== undefined)) return undefined; | |
| 47 | const key = JSON.stringify([BOOT, stylesheetAsset().path, shellAsset().path, beaconAsset().path, spriteAsset().path, Bun.hash(script), ctx.theme ?? null, ctx.url.origin, ctx.url.pathname + ctx.url.search, signInHref(ctx), signUpHref(ctx) ?? null, paletteStamp(ctx), inputs], (_k, v) => (typeof v === "bigint" ? v.toString() : v)); | |
| 48 | return `W/"${Bun.hash(key).toString(36)}"`; | |
| 49 | } | |
| 50 | ||
| 51 | /** | |
| 52 | * The 304 a reader who holds `tag` is owed, or undefined when the page must be drawn: the headers | |
| 53 | * the page itself would carry (`documentHeaders`), without its body. `script` is the page's own | |
| 54 | * script, as it passes it to `page()`. | |
| 55 | */ | |
| 56 | export function heldCopy(ctx: Ctx, tag: string | undefined, script?: string): Response | undefined { | |
| 57 | if (tag === undefined) return undefined; | |
| 58 | const lastModified = pageStamp(ctx.app, `${ctx.url.pathname}${ctx.url.search}`, tag); | |
| 59 | if (!unchanged(ctx.request, tag, lastModified)) return undefined; | |
| 60 | const headers = documentHeaders({ ctx, ...(script === undefined ? {} : { scriptJs: script }) }); | |
| 61 | headers.set("etag", tag); | |
| 62 | headers.set("last-modified", lastModified); | |
| 63 | return securityFloor(new Response(null, { status: 304, headers })); | |
| 64 | } |
src/web/studio/explore.ts +12 −2
| @@ -35,6 +35,7 @@ import { offersSignup, pageHead } from "../chrome.ts"; | ||
| 35 | 35 | import { html, type Html } from "../html.ts"; |
| 36 | 36 | import { compact } from "../compact.ts"; |
| 37 | 37 | import { page, redirect } from "../layout.ts"; |
| 38 | import { heldCopy, pageTag } from "../page_tag.ts"; | |
| 38 | 39 | import { btn, card, empty, nameMark, row as rowOf, seg, tabs, time } from "../ui.ts"; |
| 39 | 40 | |
| 40 | 41 | const href = (query: ExploreQuery, page = 1): string => { |
| @@ -239,8 +240,17 @@ export async function explorePage(ctx: Ctx): Promise<Response> { | ||
| 239 | 240 | const oldLink = ctx.url.searchParams.has("cursor"); |
| 240 | 241 | const weights = frontWeights(ctx.app.db); |
| 241 | 242 | notePhase(ctx.request, "read", performance.now() - ranking); |
| 242 | const rendering = performance.now(); | |
| 243 | 243 | const now = Date.now(); |
| 244 | // The tag is of what the page would say, before any of it is drawn: a reader who holds it gets a | |
| 245 | // 304 and no render (a refused parameter is a 400, never held). Each row's age wording is an | |
| 246 | // input, so the tag moves when "shipped 5m ago" would. | |
| 247 | const tag = refused === null ? pageTag(ctx, [line, query, shown, note, suggestion ?? null, oldLink, queryTooLong(ctx.url.searchParams.get("q")), weights, shown.items.map((r) => [whyOf(r, query.sort, now, weights)?.text ?? null, r.shippedAt === null ? null : shippedAgo(r.shippedAt, now)])]) : undefined; | |
| 248 | const held = heldCopy(ctx, tag); | |
| 249 | if (held !== undefined) { | |
| 250 | notePhase(ctx.request, "render", 0); | |
| 251 | return held; | |
| 252 | } | |
| 253 | const rendering = performance.now(); | |
| 244 | 254 | const kinds = [ |
| 245 | 255 | { label: "All", kind: "all" as const }, |
| 246 | 256 | { label: "Apps", kind: "app" as const }, |
| @@ -340,7 +350,7 @@ export async function explorePage(ctx: Ctx): Promise<Response> { | ||
| 340 | 350 | pad: false, |
| 341 | 351 | extra: "bare", |
| 342 | 352 | })}${pager(query, shown.page, shown.pages)}</div>`; |
| 343 | const drawn = page({ ctx, title: "Explore", description: "Public apps, forges and people on this forge, ranked by use, forks, deployments and recency.", body: compact(body), feed: { href: "/explore.atom", title: "Explore" } }); | |
| 353 | const drawn = page({ ctx, title: "Explore", description: "Public apps, forges and people on this forge, ranked by use, forks, deployments and recency.", body: compact(body), feed: { href: "/explore.atom", title: "Explore" }, ...(tag === undefined ? {} : { etag: tag }) }); | |
| 344 | 354 | notePhase(ctx.request, "render", performance.now() - rendering); |
| 345 | 355 | return drawn; |
| 346 | 356 | } |
src/web/studio/home.ts +18 −8
| @@ -40,6 +40,7 @@ import { field, readForm, type Form } from "../form.ts"; | ||
| 40 | 40 | import { liveRegionsJs } from "../client.ts"; |
| 41 | 41 | import { html, when, type Html } from "../html.ts"; |
| 42 | 42 | import { fragment, page, redirect } from "../layout.ts"; |
| 43 | import { heldCopy, pageTag } from "../page_tag.ts"; | |
| 43 | 44 | import { startRefusalIn, startRefusalWords, startWaitIn } from "../start_refusal.ts"; |
| 44 | 45 | import { SITE_WHAT } from "../site_line.ts"; |
| 45 | 46 | import { siteStructuredData } from "../structured_data.ts"; |
| @@ -57,6 +58,7 @@ import { | ||
| 57 | 58 | icon, |
| 58 | 59 | row, |
| 59 | 60 | time, |
| 61 | timeAgo, | |
| 60 | 62 | copyField, |
| 61 | 63 | } from "../ui.ts"; |
| 62 | 64 | import { |
| @@ -706,18 +708,14 @@ export function studioDoor(ctx: Ctx): Response { | ||
| 706 | 708 | // The door draws what is in memory; a pack pushed since is picked up for the next visit. |
| 707 | 709 | void refreshCatalogQuietly(ctx.app, 5_000); |
| 708 | 710 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); |
| 709 | let tiles: Html[] = []; | |
| 710 | 711 | const front = timePhase(ctx.request, "shelf", () => frontPage(ctx.app, Date.now(), ctx.request)); |
| 711 | 712 | const featured = front.featured; |
| 712 | 713 | const all = [...(featured === undefined ? [] : [featured]), ...front.shelf]; |
| 713 | 714 | const made = all.slice(0, Math.max(DOOR_TILES, all.filter((e) => e.pinned).length)); |
| 714 | 715 | const { mode, plans: allPlans, links, startUsd } = getDoorSettings(ctx.app.db); |
| 715 | 716 | const accounts = !accountsClosedIn(ctx.app.db, mode); |
| 716 | timePhase(ctx.request, "tiles", () => { | |
| 717 | tiles = made.map((e, at) => | |
| 718 | tile(e, at, accounts, e === featured ? html`role="region" aria-label="Featured"` : undefined), | |
| 719 | ); | |
| 720 | }); | |
| 717 | const drawTiles = (): Html[] => | |
| 718 | timePhase(ctx.request, "tiles", () => made.map((e, at) => tile(e, at, accounts, e === featured ? html`role="region" aria-label="Featured"` : undefined))); | |
| 721 | 719 | // Render is what is left of the page once the shelf read is taken out. |
| 722 | 720 | const rendering = performance.now(); |
| 723 | 721 | const visitor = ctx.auth.user === undefined; |
| @@ -734,7 +732,18 @@ export function studioDoor(ctx: Ctx): Response { | ||
| 734 | 732 | const prompt = agentPrompt(origin); |
| 735 | 733 | // A claimed hosted forge is someone's: its stranger is told whose, not sold the product (#13795). |
| 736 | 734 | const owner = visitor && ctx.url.pathname === "/" ? forgeOwner(ctx.app.db, ctx.app.config.dataDir) : undefined; |
| 737 | if (owner !== undefined) return claimedDoor(ctx, owner, tiles); | |
| 735 | if (owner !== undefined) return claimedDoor(ctx, owner, drawTiles()); | |
| 736 | // The tag is of what the door would say, before the tiles or the page are drawn: a reader who | |
| 737 | // holds it gets a 304 and no render. The ages in "Just finished" are inputs, as is the page's own | |
| 738 | // script (the tag is of the whole document); `/front` is an admin's and never tagged. | |
| 739 | const script = `${liveClientJs}\n${liveRegionsJs}\n${funnelJs}\n${startNameJs}`; | |
| 740 | const tag = visitor && ctx.url.pathname === "/" ? pageTag(ctx, [front, front.pulse.changes.map((c) => timeAgo(c.mergedAt)), mode, allPlans, links, startUsd, accounts, refusal ?? null, keptName, startWaitIn(ctx.url.searchParams) ?? null, origin, signup, catalog().packs, running().version], script) : undefined; | |
| 741 | const held = heldCopy(ctx, tag, script); | |
| 742 | if (held !== undefined) { | |
| 743 | notePhaseRest(ctx.request, "render", rendering, []); | |
| 744 | return held; | |
| 745 | } | |
| 746 | const tiles = drawTiles(); | |
| 738 | 747 | const body = html`<div class="door"> |
| 739 | 748 | <section class="door-hero"> |
| 740 | 749 | <h1>Software you ask for, reviewed and shipped.<br /><span class="muted">Yours to keep.</span></h1> |
| @@ -861,7 +870,8 @@ export function studioDoor(ctx: Ctx): Response { | ||
| 861 | 870 | title: "Software you ask for, reviewed and shipped", |
| 862 | 871 | description: SITE_WHAT, |
| 863 | 872 | body, |
| 864 | scriptJs: `${liveClientJs}\n${liveRegionsJs}\n${funnelJs}\n${startNameJs}`, | |
| 873 | scriptJs: script, | |
| 874 | ...(tag === undefined ? {} : { etag: tag }), | |
| 865 | 875 | jsonLd: siteStructuredData(origin, running().version), |
| 866 | 876 | ...(visitor |
| 867 | 877 | ? { |
test/held-copy.test.ts added +116
| @@ -0,0 +1,116 @@ | ||
| 1 | /** | |
Add the stale-304 tests described in the review body: tag must change when rows/inputs change, and the / 304 must carry the CSP and link headers. |
||
| 2 | * / and /explore answer a held copy with 304 from their inputs, before page() draws anything | |
| 3 | * (#14411). | |
| 4 | */ | |
| 5 | import { afterAll, beforeAll, describe, expect, spyOn, test } from "bun:test"; | |
| 6 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 7 | import { tmpdir } from "node:os"; | |
| 8 | import { join } from "node:path"; | |
| 9 | import { createApp, type App } from "../src/app.ts"; | |
| 10 | import { createRepo } from "../src/actions/repos.ts"; | |
| 11 | import { createApp as insertApp } from "../src/core/apps.ts"; | |
| 12 | import { createToken } from "../src/core/tokens.ts"; | |
| 13 | import { createUser } from "../src/core/users.ts"; | |
| 14 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 15 | import * as layout from "../src/web/layout.ts"; | |
| 16 | ||
| 17 | describe("a held copy of / and /explore", () => { | |
| 18 | let dataDir: string; | |
| 19 | let app: App; | |
| 20 | let servers: Servers; | |
| 21 | let token: string; | |
| 22 | let owner: Parameters<typeof createRepo>[1]; | |
| 23 | ||
| 24 | beforeAll(async () => { | |
| 25 | dataDir = await mkdtemp(join(tmpdir(), "forge-held-copy-")); | |
| 26 | app = createApp({ dataDir, port: 0, forgeRepo: "alice/forge" }); | |
| 27 | servers = await serve(app); | |
| 28 | const u = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true }); | |
| 29 | if (!u.ok) throw new Error(u.error); | |
| 30 | owner = u.value; | |
| 31 | token = createToken(app.db, u.value.id, "t", "rw").token; | |
| 32 | const made = await createRepo(app, u.value, { name: "garden", visibility: "public" }); | |
| 33 | if (!made.ok) throw new Error(String(made.error)); | |
| 34 | }, 30_000); | |
| 35 | ||
| 36 | afterAll(async () => { | |
| 37 | servers.stop(); | |
| 38 | app.close(); | |
| 39 | await rm(dataDir, { recursive: true, force: true }); | |
| 40 | }); | |
| 41 | ||
| 42 | for (const path of ["/", "/explore", "/explore?q=kind%3Aapp"]) { | |
| 43 | test(`${path}: the tag held back is a 304, and page() is not called`, async () => { | |
| 44 | const first = await fetch(`${servers.url}${path}`); | |
| 45 | expect(first.status).toBe(200); | |
| 46 | const etag = first.headers.get("etag"); | |
| 47 | expect(etag).not.toBeNull(); | |
| 48 | await first.text(); | |
| 49 | const spy = spyOn(layout, "page"); | |
| 50 | try { | |
| 51 | const again = await fetch(`${servers.url}${path}`, { headers: { "if-none-match": etag! } }); | |
| 52 | expect(again.status).toBe(304); | |
| 53 | expect(again.headers.get("etag")).toBe(etag!); | |
| 54 | expect(again.headers.get("x-content-type-options")).toBe("nosniff"); | |
| 55 | expect(again.headers.get("cache-control")).toContain("s-maxage"); | |
| 56 | expect(spy).not.toHaveBeenCalled(); | |
| 57 | // A tag that is not the page's is drawn in full. | |
| 58 | const other = await fetch(`${servers.url}${path}`, { headers: { "if-none-match": 'W/"nope"' } }); | |
| 59 | expect(other.status).toBe(200); | |
| 60 | expect(spy).toHaveBeenCalled(); | |
| 61 | } finally { | |
| 62 | spy.mockRestore(); | |
| 63 | } | |
| 64 | }); | |
| 65 | } | |
| 66 | ||
| 67 | test("/explore: a different filter is a different tag", async () => { | |
| 68 | const a = await fetch(`${servers.url}/explore`); | |
| 69 | const b = await fetch(`${servers.url}/explore?q=kind%3Aperson`); | |
| 70 | expect(a.headers.get("etag")).not.toBe(b.headers.get("etag")); | |
| 71 | }); | |
| 72 | ||
| 73 | test("/explore: a new public repo moves the tag, and the old tag is drawn in full", async () => { | |
| 74 | const first = (await fetch(`${servers.url}/explore`)).headers.get("etag")!; | |
| 75 | const made = await createRepo(app, owner, { name: "orchard", visibility: "public" }); | |
| 76 | if (!made.ok) throw new Error(String(made.error)); | |
| 77 | const listed = insertApp(app.db, { repoId: made.value.id, slug: "orchard-app", access: "public" }); | |
| 78 | if (!listed.ok) throw new Error(listed.error); | |
| 79 | const again = await fetch(`${servers.url}/explore`, { headers: { "if-none-match": first } }); | |
| 80 | expect(again.status).toBe(200); | |
| 81 | expect(again.headers.get("etag")).not.toBe(first); | |
| 82 | expect(await again.text()).toContain("orchard"); | |
| 83 | }); | |
| 84 | ||
| 85 | test("/: a door setting moves the tag, and the old tag is drawn in full", async () => { | |
| 86 | const first = (await fetch(`${servers.url}/`)).headers.get("etag")!; | |
| 87 | app.db.run("INSERT INTO forge_setting (key, value, updated_by, updated_at) VALUES ('signup', 'closed', ?1, 1) ON CONFLICT(key) DO UPDATE SET value = excluded.value", [owner!.id]); | |
| 88 | const again = await fetch(`${servers.url}/`, { headers: { "if-none-match": first } }); | |
| 89 | expect(again.status).toBe(200); | |
| 90 | expect(again.headers.get("etag")).not.toBe(first); | |
| 91 | }); | |
| 92 | ||
| 93 | test("/ and /explore: the theme moves the tag", async () => { | |
| 94 | for (const path of ["/", "/explore"]) { | |
| 95 | const plain = (await fetch(`${servers.url}${path}`)).headers.get("etag")!; | |
| 96 | const dark = await fetch(`${servers.url}${path}`, { headers: { cookie: "forge_theme=dark", "if-none-match": plain } }); | |
| 97 | expect(dark.status).toBe(200); | |
| 98 | expect(dark.headers.get("etag")).not.toBe(plain); | |
| 99 | } | |
| 100 | }); | |
| 101 | ||
| 102 | test("/: the 304 carries the headers the 200 does", async () => { | |
| 103 | const full = await fetch(`${servers.url}/`); | |
| 104 | await full.text(); | |
| 105 | const held = await fetch(`${servers.url}/`, { headers: { "if-none-match": full.headers.get("etag")! } }); | |
| 106 | expect(held.status).toBe(304); | |
| 107 | for (const name of ["content-security-policy", "link", "cache-control", "vary", "referrer-policy", "x-content-type-options", "speculation-rules"]) | |
| 108 | expect(held.headers.get(name)).toBe(full.headers.get(name)); | |
| 109 | expect(full.headers.get("content-security-policy")).not.toBeNull(); | |
| 110 | }); | |
| 111 | ||
| 112 | test("a signed-in page is never tagged", async () => { | |
| 113 | const res = await fetch(`${servers.url}/explore`, { headers: { authorization: `Bearer ${token}` } }); | |
| 114 | expect(res.headers.get("etag")).toBeNull(); | |
| 115 | }); | |
| 116 | }); |
new above
offline
Non-blocking: two JSDoc blocks sit back to back here; fold the second into the first.