Public: anyone can read this; the crew can change it.
An asset served under /assets/ with a content-hash name is cached for a year as immutable, and an unhashed one is not #13478
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/13463-an-asset-served-under-assets-with-a-cont → main 1 commit fork 1 thread · 1 unresolved
1 file
changed +23
−0
test/assets.test.ts +23
| @@ -94,3 +94,26 @@ test("boot keeps this release's assets for the next one", async () => { | ||
| 94 | 94 | ] as const) |
| 95 | 95 | expect(await Bun.file(join(keptDir(), `${asset.name}.${ext}`)).exists()).toBe(true); |
| 96 | 96 | }); |
| 97 | ||
| 98 | test("a conditional request with the matching ETag answers 304, a different one 200", async () => { | |
| 99 | for (const asset of [stylesheetAsset(), shellAsset(), beaconAsset(), spriteAsset()]) { | |
| 100 | const url = `${servers.url}${asset.path}`; | |
| 101 | const hit = await fetch(url, { headers: { "if-none-match": asset.etag } }); | |
| 102 | expect(hit.status).toBe(304); | |
| 103 | expect(await hit.text()).toBe(""); | |
| 104 | expect(hit.headers.get("etag")).toBe(asset.etag); | |
| 105 | expect(hit.headers.get("cache-control")).toBe(IMMUTABLE); | |
| 106 | const miss = await fetch(url, { headers: { "if-none-match": '"other"' } }); | |
| 107 | expect(miss.status).toBe(200); | |
| 108 | } | |
| 109 | }); | |
| 110 | ||
| 111 | test("an unhashed or unknown asset path is never immutable", async () => { | |
| 112 | const paths = ["/assets/forge.css", "/assets/shell.js", "/assets/nope.js", `/assets/forge-${"f".repeat(16)}.css`, "/assets/", "/assets/x.txt"]; | |
| 113 | for (const path of paths) { | |
| 114 | const res = await fetch(`${servers.url}${path}`, { redirect: "manual" }); | |
| 115 | expect(res.status).not.toBe(200); | |
| 116 | expect(res.headers.get("cache-control") ?? "").not.toContain("immutable"); | |
| 117 | expect(res.headers.get("cache-control") ?? "").not.toContain("max-age=31536000"); | |
| 118 | } | |
| 119 | }); |
new above
offline
Nit: this line is long; wrap the paths array one per line. Also pin the expected status (404) instead of
not.toBe(200)so a stray 301/500 does not pass.