From 2fdc064ac836d033912da37daf610d5a2a2d01fc Mon Sep 17 00:00:00 2001 From: Adam <2363879+adamdotdevin@users.noreply.github.com> Date: Fri, 6 Nov 2026 15:21:29 -0600 Subject: [PATCH] fix(site): keep earlier builds' hashed assets after a deploy (#62431) --- services/site/README.md | 5 + services/site/script/build.ts | 3 + services/site/script/retain-assets.ts | 99 ++++++++++++++++++++ services/site/test/retain-assets.test.ts | 114 +++++++++++++++++++++++ 4 files changed, 221 insertions(+) create mode 100644 services/site/script/retain-assets.ts create mode 100644 services/site/test/retain-assets.test.ts diff --git a/services/site/README.md b/services/site/README.md index befbf64147d..25d1517f0af 100644 --- a/services/site/README.md +++ b/services/site/README.md @@ -48,6 +48,11 @@ dev.opencode.ai, `opencode-site-production` on opencode.ai), its bindings and it pushes to `dev` and `production` (`.github/workflows/deploy-site.yml`). Production takes no routes until `productionOwnsRoot` is switched on. +Each build also carries the hashed scripts and stylesheets of the last 20 builds it replaces (for up to a +week), downloaded from the live site's `/_build/retained.json`, so pages already open keep loading their +code after a deploy (`script/retain-assets.ts`). A rollback in the Cloudflare dashboard restores the older +version's assets only, so tabs opened on the rolled-back build reload once. + The V2 docs at `/docs` are part of this app (`src/docs/`, see `src/docs/AGENTS.md`). `script/build.ts` prerenders them into the Worker's assets after `vite build`. A `/docs` URL without a V2 page redirects to the V1 docs app on docs.opencode.ai. diff --git a/services/site/script/build.ts b/services/site/script/build.ts index 1dbd181a3b6..af67b50797f 100644 --- a/services/site/script/build.ts +++ b/services/site/script/build.ts @@ -16,6 +16,7 @@ import { import { resolveAndParseConfig } from "@cloudflare/config" import { mediaFile } from "../src/lib/media" import { stageHeaders } from "../src/lib/noindex" +import { retainAssets } from "./retain-assets" const root = path.resolve(import.meta.dir, "..") const modeIndex = process.argv.indexOf("--mode") @@ -71,6 +72,8 @@ if (fixed.length) throw new Error(`fixed-URL files missing from Worker assets: $ // The stage's headers for the assets the Worker never sees; its own responses get them from src/lib/noindex.ts. const headers = Object.entries(stageHeaders(mode)).map(([name, value]) => ` ${name}: ${value}\n`) if (headers.length) await appendFile(path.join(assets, "_headers"), `\n/*\n${headers.join("")}`) +// Open pages from earlier builds keep loading their code after this deploy (script/retain-assets.ts). +await retainAssets(assets, mode === "dev" ? "https://dev.opencode.ai" : "https://opencode.ai") await writeAssets({ root, sourceDirectory: assets }) const bundle = getWorkerBundleDir(root) diff --git a/services/site/script/retain-assets.ts b/services/site/script/retain-assets.ts new file mode 100644 index 00000000000..668eb8227ce --- /dev/null +++ b/services/site/script/retain-assets.ts @@ -0,0 +1,99 @@ +// Keeps the hashed scripts, stylesheets and fonts of the last 20 builds a deploy replaced in its +// assets, each for up to a week after it stopped being live, so a page rendered by an earlier build +// still loads the code for the next page it opens. Workers Static Assets serve only the deployed version's files and have no +// skew protection, so without this every deploy turned the open tabs' next navigation into a +// missing chunk and a full reload. +// +// The live site lists what it serves in /_build/retained.json: its own hashed files (`live`) and the +// earlier builds' it still carries (`retired`, each with when it stopped being live). script/build.ts +// calls this to read that list, download the carried files from the live site into the new build's +// assets and write the next list. Cloudflare stores assets by content hash, so carried files add +// entries to the upload manifest but no upload. If the list can't be read (the first deploy, an +// outage, a local build offline), the build carries nothing and goes on. +import { mkdir } from "node:fs/promises" +import path from "node:path" +import { z } from "zod" + +/** How long a file stays after the last build that served it was replaced, for at most `builds` replaced builds. */ +const retention = 7 * 24 * 60 * 60 * 1000 +const builds = 20 +/** Carrying never holds up a deploy longer than this; what hasn't downloaded by then is left behind. */ +const deadline = 3 * 60 * 1000 +/** Files named by a hash of their contents: immutable in public/_headers, safe to keep beside newer builds. */ +const hashed = /^(_build\/assets\/|docs\/_build\/|textjs\/(chunk|lite|picker|yoga)-)[\w./-]+$/ +const ledgerPath = "_build/retained.json" + +const Ledger = z.object({ + live: z.array(z.string()), + retired: z.record(z.string(), z.string()), +}) + +/** Carries the files `origin` still needs from earlier builds into `assets` and writes the next list. */ +export async function retainAssets(assets: string, origin: string) { + const live = (await Array.fromAsync(new Bun.Glob("**/*").scan({ cwd: assets }))) + .map((file) => file.replaceAll("\\", "/")) + .filter((file) => hashed.test(file)) + const own = new Set(live) + const signal = AbortSignal.timeout(deadline) + const previous = await readLedger(origin, signal) + const now = new Date() + const candidates = new Map([ + ...Object.entries(previous.retired), + // The live build stops being live with this deploy. + ...previous.live.map((file) => [file, now.toISOString()] as const), + ]) + // Every deploy retires its files at one time, so the times name the replaced builds. + const kept = new Set( + [...new Set(candidates.values())] + .filter((at) => now.getTime() - Date.parse(at) < retention) + .toSorted() + .slice(-builds), + ) + const wanted = [...candidates].filter( + ([file, at]) => kept.has(at) && hashed.test(file) && !file.split("/").includes("..") && !own.has(file), + ) + const fetched = await mapLimit(wanted, 16, async ([file, at]) => { + const bytes = await download(`${origin}/${file}`, signal) + if (!bytes) return + await mkdir(path.dirname(path.join(assets, file)), { recursive: true }) + await Bun.write(path.join(assets, file), bytes) + return { file, at, bytes: bytes.length } + }) + const retired = fetched.filter((entry) => entry !== undefined) + await Bun.write( + path.join(assets, ledgerPath), + JSON.stringify({ live, retired: Object.fromEntries(retired.map((entry) => [entry.file, entry.at])) }), + ) + const megabytes = (retired.reduce((total, entry) => total + entry.bytes, 0) / 1e6).toFixed(1) + const missing = wanted.length - retired.length + console.log( + `retain-assets: ${live.length} live, carried ${retired.length} from earlier builds (${megabytes} MB)` + + (missing ? `; ${missing} couldn't be fetched from ${origin}` : ""), + ) +} + +async function readLedger(origin: string, signal: AbortSignal) { + const bytes = await download(`${origin}/${ledgerPath}?t=${Date.now()}`, signal) + const ledger = Ledger.safeParse(bytes && (await new Response(bytes).json().catch(() => undefined))) + if (ledger.success) return ledger.data + console.warn(`retain-assets: ${origin}/${ledgerPath} missing or unreadable; carrying nothing from earlier builds`) + return { live: [], retired: {} } +} + +/** The file's bytes, trying twice; undefined once it's gone (404), still failing or past the deadline. */ +async function download(url: string, signal: AbortSignal, attempt = 1): Promise | undefined> { + const response = await fetch(url, { signal, cache: "no-store" }).catch(() => undefined) + const bytes = response?.ok ? await response.bytes().catch(() => undefined) : undefined + if (bytes || response?.status === 404 || attempt === 2 || signal.aborted) return bytes + return download(url, signal, attempt + 1) +} + +async function mapLimit(items: T[], limit: number, run: (item: T) => Promise) { + const results: R[] = [] + const queue = items.entries() + const worker = async () => { + for (const [index, item] of queue) results[index] = await run(item) + } + await Promise.all(Array.from({ length: Math.min(limit, items.length) }, worker)) + return results +} diff --git a/services/site/test/retain-assets.test.ts b/services/site/test/retain-assets.test.ts new file mode 100644 index 00000000000..343615921c7 --- /dev/null +++ b/services/site/test/retain-assets.test.ts @@ -0,0 +1,114 @@ +import { afterEach, expect, test } from "bun:test" +import { mkdtemp, rm } from "node:fs/promises" +import { tmpdir } from "node:os" +import path from "node:path" +import { retainAssets } from "../script/retain-assets" + +const day = 24 * 60 * 60 * 1000 +const dirs: string[] = [] +afterEach(() => Promise.all(dirs.splice(0).map((dir) => rm(dir, { recursive: true, force: true })))) + +async function build(files: Record) { + const dir = await mkdtemp(path.join(tmpdir(), "retain-assets-")) + dirs.push(dir) + await Promise.all(Object.entries(files).map(([file, text]) => Bun.write(path.join(dir, file), text))) + return dir +} + +function serve(files: Record) { + return Bun.serve({ + port: 0, + fetch: (request) => { + const file = files[new URL(request.url).pathname.slice(1)] + return file === undefined ? new Response("not found", { status: 404 }) : new Response(file) + }, + }) +} + +test("carries the replaced build's hashed files and the earlier ones still in their window", async () => { + const recent = new Date(Date.now() - day).toISOString() + const expired = new Date(Date.now() - 8 * day).toISOString() + using server = serve({ + "_build/retained.json": JSON.stringify({ + live: ["_build/assets/page-old.js", "_build/assets/shared-same.js", "docs/_build/docs-old.css"], + retired: { + "_build/assets/older-recent.js": recent, + "_build/assets/oldest-expired.js": expired, + "_build/assets/gone.js": recent, + }, + }), + "_build/assets/page-old.js": "old page", + "_build/assets/shared-same.js": "shared", + "docs/_build/docs-old.css": "old docs", + "_build/assets/older-recent.js": "older", + "_build/assets/oldest-expired.js": "oldest", + }) + const assets = await build({ + "_build/assets/page-new.js": "new page", + "_build/assets/shared-same.js": "shared", + "index.html": "", + }) + + await retainAssets(assets, server.url.origin) + + expect(await Bun.file(path.join(assets, "_build/assets/page-old.js")).text()).toBe("old page") + expect(await Bun.file(path.join(assets, "docs/_build/docs-old.css")).text()).toBe("old docs") + expect(await Bun.file(path.join(assets, "_build/assets/older-recent.js")).text()).toBe("older") + expect(await Bun.file(path.join(assets, "_build/assets/oldest-expired.js")).exists()).toBe(false) + const ledger = await Bun.file(path.join(assets, "_build/retained.json")).json() + expect(ledger.live.toSorted()).toEqual(["_build/assets/page-new.js", "_build/assets/shared-same.js"]) + expect(Object.keys(ledger.retired).toSorted()).toEqual([ + "_build/assets/older-recent.js", + "_build/assets/page-old.js", + "docs/_build/docs-old.css", + ]) + expect(ledger.retired["_build/assets/older-recent.js"]).toBe(recent) + expect(Date.now() - Date.parse(ledger.retired["_build/assets/page-old.js"])).toBeLessThan(60_000) +}) + +test("keeps at most the last 20 replaced builds", async () => { + const retired = Object.fromEntries( + Array.from({ length: 21 }, (_, index) => [ + `_build/assets/build-${index}.js`, + new Date(Date.now() - (21 - index) * 60_000).toISOString(), + ]), + ) + using server = serve({ + "_build/retained.json": JSON.stringify({ live: ["_build/assets/_..-live.css"], retired }), + "_build/assets/_..-live.css": "live", + ...Object.fromEntries(Object.keys(retired).map((file) => [file, file])), + }) + const assets = await build({ "index.html": "" }) + + await retainAssets(assets, server.url.origin) + + const kept = Object.keys((await Bun.file(path.join(assets, "_build/retained.json")).json()).retired) + expect(kept).toHaveLength(20) + expect(kept).toContain("_build/assets/_..-live.css") + expect(kept).not.toContain("_build/assets/build-0.js") + expect(kept).not.toContain("_build/assets/build-1.js") + expect(await Bun.file(path.join(assets, "_build/assets/build-1.js")).exists()).toBe(false) +}) + +test("carries nothing when the live site has no list, and never files that aren't hashed", async () => { + using empty = serve({}) + const first = await build({ "_build/assets/a.js": "a", "index.html": "" }) + await retainAssets(first, empty.url.origin) + expect(await Bun.file(path.join(first, "_build/retained.json")).json()).toEqual({ + live: ["_build/assets/a.js"], + retired: {}, + }) + + using garbled = serve({ "_build/retained.json": "" }) + await retainAssets(first, garbled.url.origin) + expect((await Bun.file(path.join(first, "_build/retained.json")).json()).retired).toEqual({}) + + using tampered = serve({ + "_build/retained.json": JSON.stringify({ live: ["index.html", "_build/assets/../../escape.js"], retired: {} }), + "index.html": "", + }) + const second = await build({ "index.html": "" }) + await retainAssets(second, tampered.url.origin) + expect(await Bun.file(path.join(second, "index.html")).text()).toBe("") + expect((await Bun.file(path.join(second, "_build/retained.json")).json()).retired).toEqual({}) +})