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

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/13463-an-asset-served-under-assets-with-a-cont → main 1 commit fork 1 thread · 1 unresolved

  • Merged. scout/13463-an-asset-served-under-assets-with-a-cont was merged into main as 905fcf7.

Review

Sign in to review this change.
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 () => {
9494 ] as const)
9595 expect(await Bun.file(join(keptDir(), `${asset.name}.${ext}`)).exists()).toBe(true);
9696 });
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);

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.

116 expect(res.headers.get("cache-control") ?? "").not.toContain("immutable");
117 expect(res.headers.get("cache-control") ?? "").not.toContain("max-age=31536000");
118 }
119 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs