Public: anyone can read this; the crew can change it.
Every answer that sends Last-Modified honours If-Modified-Since, or stops sending it #16685
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/14415-every-answer-that-sends-last-modified-ho → main 0 commits fork 2 threads · 2 unresolved
4 files
changed +68
−8
src/http/page_stamp.ts +5 −2
| @@ -42,8 +42,11 @@ export function pageStamp(app: object, url: string, etag: string, now: number = | ||
| 42 | 42 | |
| 43 | 43 | /** |
| 44 | 44 | * The date this URL was last stamped with, for a page that answers its own 304 before rendering. |
| 45 | * Only while the bytes are still the ones stamped: when `etag` is not the tag the stamp was made | |
| 46 | * for, the page has changed since it was last sent, and an old date must not match it. | |
| 45 | 47 | */ |
| 46 | export function heldStamp(app: object, url: string): string | undefined { | |
| 48 | export function heldStamp(app: object, url: string, etag: string): string | undefined { | |
| 47 | 49 | const held = byApp.get(app)?.get(url); |
| 48 | return held === undefined ? undefined : new Date(held.second * 1000).toUTCString(); | |
| 50 | if (held === undefined || held.etag !== etag) return undefined; | |
| 51 | return new Date(held.second * 1000).toUTCString(); | |
| 49 | 52 | } |
src/web/pages/changelog.ts +8 −5
| @@ -6,6 +6,7 @@ | ||
| 6 | 6 | * a new commit. Public and server-rendered. |
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | import { unchanged } from "../../http/etag.ts"; | |
| 9 | 10 | import { heldStamp } from "../../http/page_stamp.ts"; |
| 10 | 11 | import { MONTH_NAMES, monthLabel } from "../../release/month.ts"; |
| 11 | 12 | import { notePhase, takeSessionTime, timePhaseAsync } from "../../core/route_timing.ts"; |
| @@ -424,7 +425,7 @@ export async function changelogPage(ctx: Ctx): Promise<Response> { | ||
| 424 | 425 | ]); |
| 425 | 426 | const body = html`${pageHead("Changelog", { sub: "What went live, as it happens." })}${nearBlock(planned === undefined ? "" : repoBase(planned.repo), near)}${filters}${betFilters}${speedBlock(speed)}${read.own !== undefined && readable ? html`<p class="small"><a href="/${read.own}/waves" data-waves-link>Read it by wave</a></p>` : null} |
| 426 | 427 | <div id="shipped" data-live="${LIVE}" aria-live="polite">${shippedRegion(read, choice, new Date(), "UTC", toLive, readable, chapters, bet, target)}</div>`; |
| 427 | const response = page({ ctx, title: "Changelog", description: "What shipped on this forge, newest first: each change that landed and when.", body, feed: { href: "/changelog.atom", title: "Changelog" }, json: { href: `/api/v1/changelog${choice === read.own ? "" : `?repo=${encodeURIComponent(choice)}`}`, title: "Changelog (JSON)" }, scriptJs: `${liveRegionsJs}\n${localDaysJs}` }); | |
| 428 | const response = page({ ctx, ...(tag === undefined ? {} : { etag: tag }), title: "Changelog", description: "What shipped on this forge, newest first: each change that landed and when.", body, feed: { href: "/changelog.atom", title: "Changelog" }, json: { href: `/api/v1/changelog${choice === read.own ? "" : `?repo=${encodeURIComponent(choice)}`}`, title: "Changelog (JSON)" }, scriptJs: `${liveRegionsJs}\n${localDaysJs}` }); | |
| 428 | 429 | notePhase(ctx.request, "render", performance.now() - rendering); |
| 429 | 430 | if (keep === undefined) return response; |
| 430 | 431 | const headers = new Headers(response.headers); |
| @@ -459,7 +460,7 @@ function older(more: string | undefined, last: string | undefined, choice: strin | ||
| 459 | 460 | * no identity at all may be kept by a shared cache; any other answer is |
| 460 | 461 | * `private`. A page that shows a flash is never tagged (the caller skips it). |
| 461 | 462 | */ |
| 462 | function cacheHeaders(ctx: Ctx, etag: string): { matched: boolean; response: Response; headers: Headers } | undefined { | |
| 463 | export function cacheHeaders(ctx: Ctx, etag: string): { matched: boolean; response: Response; headers: Headers } | undefined { | |
| 463 | 464 | if (ctx.request.method !== "GET" && ctx.request.method !== "HEAD") return undefined; |
| 464 | 465 | const h = ctx.request.headers; |
| 465 | 466 | const anonymous = !h.has("cookie") && !h.has("authorization") && !h.has("x-plat-user") && !h.has("x-plat-edge-token") && ctx.auth.user === undefined; |
| @@ -468,9 +469,11 @@ function cacheHeaders(ctx: Ctx, etag: string): { matched: boolean; response: Res | ||
| 468 | 469 | headers.set("cache-control", `public, max-age=0, s-maxage=${EDGE_FRESH}, stale-while-revalidate=${EDGE_STALE}`); |
| 469 | 470 | headers.set("vary", EDGE_VARY); |
| 470 | 471 | } else headers.set("cache-control", "private, no-cache"); |
| 471 | const matched = holds(h.get("if-none-match"), etag); | |
| 472 | // The date the page was last sent with, so a 304 here says what the page's own would. | |
| 473 | const stamped = matched ? heldStamp(ctx.app, `${ctx.url.pathname}${ctx.url.search}`) : undefined; | |
| 472 | // The date the page was last sent with, so a 304 here says what the page's own would, and a | |
| 473 | // reader behind the edge that strips ETags (`unchanged`) can revalidate by date. | |
| 474 | const held = heldStamp(ctx.app, `${ctx.url.pathname}${ctx.url.search}`, etag); | |
| 475 | const matched = unchanged(ctx.request, etag, held); | |
| 476 | const stamped = matched ? held : undefined; | |
| 474 | 477 | if (stamped !== undefined) headers.set("last-modified", stamped); |
| 475 | 478 | // The 304 carries the floor headers; the stored copy keeps the CSP it came with. |
| 476 | 479 | const response = matched ? securityFloor(new Response(null, { status: 304, headers: new Headers(headers) })) : new Response(null); |
src/web/pages/kept_page.ts +1 −1
| @@ -71,7 +71,7 @@ const IDENTITY = ["cookie", "authorization", "x-plat-user", "x-plat-edge-token"] | ||
| 71 | 71 | export function keptPageKey(ctx: Ctx, repoId: string, ...parts: string[]): string | undefined { |
| 72 | 72 | const r = ctx.request; |
| 73 | 73 | if (ctx.auth.user !== undefined || r.method !== "GET" || ctx.theme !== undefined) return undefined; |
| 74 | if (r.headers.has("if-none-match") || r.headers.has("range") || readFlash(r) !== undefined) return undefined; | |
| 74 | if (r.headers.has("if-none-match") || r.headers.has("if-modified-since") || r.headers.has("range") || readFlash(r) !== undefined) return undefined; | |
| 75 | 75 | const encoding = negotiate(r.headers.get("accept-encoding")); |
| 76 | 76 | if (encoding === null) return undefined; |
| 77 | 77 | const identity = IDENTITY.map((name) => (r.headers.has(name) ? "1" : "0")).join(""); |
test/last-modified-held.test.ts added +54
| @@ -0,0 +1,54 @@ | ||
| 1 | /** | |
| 2 | * Every answer that sends Last-Modified honours If-Modified-Since (#14415): the changelog page | |
| 3 | * (tagged by its own reading, then kept) answers a dated repeat with 304 and no body. | |
| 4 | */ | |
| 5 | import { afterAll, beforeAll, expect, 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 { createUser } from "../src/core/users.ts"; | |
| 12 | import { cacheHeaders } from "../src/web/pages/changelog.ts"; | |
| 13 | import { pageStamp } from "../src/http/page_stamp.ts"; | |
| 14 | import { keptPageKey } from "../src/web/pages/kept_page.ts"; | |
| 15 | import type { Ctx } from "../src/http/context.ts"; | |
| 16 | ||
| 17 | let dataDir: string; | |
| 18 | let app: App; | |
| 19 | ||
| 20 | beforeAll(async () => { | |
| 21 | dataDir = await mkdtemp(join(tmpdir(), "forge-lastmod-")); | |
| 22 | app = createApp({ dataDir, port: 0, forgeRepo: "alice/forge" }); | |
| 23 | const u = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true }); | |
| 24 | if (!u.ok) throw new Error(u.error); | |
| 25 | const made = await createRepo(app, u.value, { name: "forge", visibility: "public" }); | |
| 26 | if (!made.ok) throw new Error(String(made.error)); | |
| 27 | }, 30_000); | |
| 28 | ||
| 29 | afterAll(async () => { | |
| 30 | app.close(); | |
| 31 | await rm(dataDir, { recursive: true, force: true }); | |
| 32 | }); | |
| 33 | ||
| 34 | const get = (headers: Record<string, string>) => ({ app, request: new Request("http://forge.test/changelog", { headers }), url: new URL("http://forge.test/changelog"), auth: {}, theme: undefined }) as unknown as Ctx; | |
| 35 | ||
| 36 | test("changelog: a date-only repeat (If-Modified-Since = the stamp, no If-None-Match) is matched", () => { | |
| 37 | const stamp = pageStamp(app, "/changelog", 'W/"a"', Date.parse("2026-01-01T00:00:10Z")); | |
| 38 | const kept = cacheHeaders(get({ "if-modified-since": stamp }), 'W/"a"')!; | |
| 39 | expect(kept.matched).toBe(true); | |
| 40 | expect(kept.response.status).toBe(304); | |
| 41 | expect(kept.response.headers.get("last-modified")).toBe(stamp); | |
| 42 | expect(cacheHeaders(get({ "if-modified-since": new Date(Date.parse(stamp) - 5000).toUTCString() }), 'W/"a"')!.matched).toBe(false); | |
| 43 | }); | |
| 44 | ||
| 45 | test("changelog: changed content with the old date is not matched (the stamp was for other bytes)", () => { | |
| 46 | const stamp = pageStamp(app, "/changelog", 'W/"a"', Date.parse("2026-01-01T00:00:10Z")); | |
| 47 | expect(cacheHeaders(get({ "if-modified-since": stamp }), 'W/"b"')!.matched).toBe(false); | |
| 48 | }); | |
| 49 | ||
| 50 | test("a kept page is never replayed to a reader who sends If-Modified-Since: it is revalidated by the page", () => { | |
| 51 | const ctx = (headers: Record<string, string>) => ({ app, request: new Request("http://forge.test/alice/forge/blob/c/a.ts", { headers }), url: new URL("http://forge.test/alice/forge/blob/c/a.ts"), auth: {}, theme: undefined }) as unknown as Ctx; | |
| 52 | expect(keptPageKey(ctx({ "accept-encoding": "br" }), "r1")).toBeDefined(); | |
| 53 | expect(keptPageKey(ctx({ "accept-encoding": "br", "if-modified-since": new Date().toUTCString() }), "r1")).toBeUndefined(); | |
| 54 | }); |
new above
offline
Needs a test that fails without this line: a date-only request (If-Modified-Since == held stamp, no If-None-Match) must give 304. The /changelog test in last-modified-held.test.ts passes on the base, so it does not cover this. Also cover a changed reading with an old date giving 200, since
heldis the previously sent stamp.