From 271d78a9a504be21ce3467e1de0a796a162a9805 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Tue, 10 Feb 2026 01:22:29 +0000 Subject: [PATCH] Set immutable cache headers for built static assets Co-authored-by: Theo Browne --- apps/t3/scripts/smoke-test.mjs | 20 ++++++++++++++++++++ apps/t3/src/cli.ts | 16 ++++++++++------ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/apps/t3/scripts/smoke-test.mjs b/apps/t3/scripts/smoke-test.mjs index 62d367bffe..491ad3c2de 100644 --- a/apps/t3/scripts/smoke-test.mjs +++ b/apps/t3/scripts/smoke-test.mjs @@ -175,6 +175,26 @@ async function main() { )}.`, ); } + const html = await page.text(); + const assetMatch = html.match(/(?:src|href)="(\/assets\/[^"]+)"/); + if (!assetMatch?.[1]) { + throw new Error("Smoke test failed: could not locate built asset path in HTML."); + } + const assetUrl = new URL(assetMatch[1], parsedAppUrl); + const assetResponse = await fetch(assetUrl); + if (assetResponse.status !== 200) { + throw new Error( + `Smoke test failed: expected built asset status 200, received ${assetResponse.status}.`, + ); + } + const assetCacheControl = (assetResponse.headers.get("cache-control") ?? "").toLowerCase(); + if (!assetCacheControl.includes("immutable")) { + throw new Error( + `Smoke test failed: expected immutable cache-control on built asset, got ${String( + assetResponse.headers.get("cache-control"), + )}.`, + ); + } const missingAssetUrl = new URL("/assets/missing-bundle.js", parsedAppUrl); const missingAsset = await fetch(missingAssetUrl); if (missingAsset.status !== 404) { diff --git a/apps/t3/src/cli.ts b/apps/t3/src/cli.ts index 1baf2af348..a11eb1d22e 100644 --- a/apps/t3/src/cli.ts +++ b/apps/t3/src/cli.ts @@ -432,6 +432,14 @@ function contentTypeFor(filePath: string): string { return "application/octet-stream"; } +function cacheControlFor(filePath: string): string { + if (filePath.endsWith(".html")) { + return "no-store"; + } + + return "public, max-age=31536000, immutable"; +} + function isPathInside(parent: string, child: string): boolean { const relative = path.relative(parent, child); return relative === "" || (!relative.startsWith("..") && !path.isAbsolute(relative)); @@ -619,9 +627,7 @@ function startStaticWebServer(distRoot: string, port: number) { response.setHeader("Content-Type", contentTypeFor(targetPath)); response.setHeader("Content-Length", String(stats.size)); response.setHeader("X-Content-Type-Options", "nosniff"); - if (targetPath.endsWith(".html")) { - response.setHeader("Cache-Control", "no-store"); - } + response.setHeader("Cache-Control", cacheControlFor(targetPath)); response.end(); }); return; @@ -637,9 +643,7 @@ function startStaticWebServer(distRoot: string, port: number) { response.setHeader("Content-Type", contentTypeFor(targetPath)); response.setHeader("X-Content-Type-Options", "nosniff"); response.setHeader("Content-Length", String(content.byteLength)); - if (targetPath.endsWith(".html")) { - response.setHeader("Cache-Control", "no-store"); - } + response.setHeader("Cache-Control", cacheControlFor(targetPath)); response.end(content); }); });