diff --git a/apps/t3/scripts/smoke-test.mjs b/apps/t3/scripts/smoke-test.mjs index a0b15b9092..f3a103c827 100644 --- a/apps/t3/scripts/smoke-test.mjs +++ b/apps/t3/scripts/smoke-test.mjs @@ -273,6 +273,16 @@ async function main() { if ((conditionalAsset.headers.get("cache-control") ?? "").toLowerCase() !== assetCacheControl) { throw new Error("Smoke test failed: expected cache-control preserved on conditional asset response."); } + const weakConditionalAsset = await fetch(assetUrl, { + headers: { + "If-None-Match": `W/${assetEtag}`, + }, + }); + if (weakConditionalAsset.status !== 304) { + throw new Error( + `Smoke test failed: expected weak conditional asset status 304, received ${weakConditionalAsset.status}.`, + ); + } const conditionalHeadAsset = await fetch(assetUrl, { method: "HEAD", headers: { diff --git a/apps/t3/src/cli.test.ts b/apps/t3/src/cli.test.ts index 7b462fb47f..b386e2ab3f 100644 --- a/apps/t3/src/cli.test.ts +++ b/apps/t3/src/cli.test.ts @@ -951,6 +951,11 @@ describe("ifNoneMatchSatisfied", () => { expect(ifNoneMatchSatisfied("\"foo\", \"bar\", \"abc\"", "\"abc\"")).toBe(true); }); + it("matches weak and strong forms using weak comparison semantics", () => { + expect(ifNoneMatchSatisfied("W/\"abc\"", "\"abc\"")).toBe(true); + expect(ifNoneMatchSatisfied("\"abc\"", "W/\"abc\"")).toBe(true); + }); + it("does not match non-identical etags", () => { expect(ifNoneMatchSatisfied("\"foo\", \"bar\"", "\"abc\"")).toBe(false); }); diff --git a/apps/t3/src/cli.ts b/apps/t3/src/cli.ts index 0168608e23..56cd0a399a 100644 --- a/apps/t3/src/cli.ts +++ b/apps/t3/src/cli.ts @@ -516,6 +516,11 @@ function staticEtagFor(stats: fs.Stats): string { return `"${stats.size.toString(16)}-${Math.trunc(stats.mtimeMs).toString(16)}"`; } +function normalizeWeakEtag(etag: string): string { + const trimmed = etag.trim(); + return trimmed.startsWith("W/") ? trimmed.slice(2).trim() : trimmed; +} + export function ifNoneMatchSatisfied(ifNoneMatchHeader: string | undefined, etag: string): boolean { if (!ifNoneMatchHeader) { return false; @@ -529,7 +534,12 @@ export function ifNoneMatchSatisfied(ifNoneMatchHeader: string | undefined, etag return false; } - return candidates.includes("*") || candidates.includes(etag); + if (candidates.includes("*")) { + return true; + } + + const normalizedEtag = normalizeWeakEtag(etag); + return candidates.some((candidate) => normalizeWeakEtag(candidate) === normalizedEtag); } export function parseByteRangeHeader(