fix: etag caching HTTP headers (#2504)
This commit is contained in:
@@ -18,6 +18,7 @@ export interface StaticFile {
|
|||||||
hash: string | null;
|
hash: string | null;
|
||||||
size: number;
|
size: number;
|
||||||
readable: ReadableStream<Uint8Array> | Uint8Array;
|
readable: ReadableStream<Uint8Array> | Uint8Array;
|
||||||
|
close(): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface BuildCache {
|
export interface BuildCache {
|
||||||
@@ -101,6 +102,7 @@ export class ProdBuildCache implements BuildCache {
|
|||||||
hash: info.hash,
|
hash: info.hash,
|
||||||
size: stat.size,
|
size: stat.size,
|
||||||
readable: file.readable,
|
readable: file.readable,
|
||||||
|
close: () => file.close(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -50,6 +50,7 @@ export class MemoryBuildCache implements DevBuildCache {
|
|||||||
hash: processed.hash,
|
hash: processed.hash,
|
||||||
readable: processed.content,
|
readable: processed.content,
|
||||||
size: processed.content.byteLength,
|
size: processed.content.byteLength,
|
||||||
|
close: () => {},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,6 +66,7 @@ export class MemoryBuildCache implements DevBuildCache {
|
|||||||
hash: null,
|
hash: null,
|
||||||
size: stat.size,
|
size: stat.size,
|
||||||
readable: file.readable,
|
readable: file.readable,
|
||||||
|
close: () => file.close(),
|
||||||
};
|
};
|
||||||
} catch (_err) {
|
} catch (_err) {
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ export function staticFiles<T>(): MiddlewareFn<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (req.method !== "GET" && req.method !== "HEAD") {
|
if (req.method !== "GET" && req.method !== "HEAD") {
|
||||||
|
file.close();
|
||||||
return new Response("Method Not Allowed", { status: 405 });
|
return new Response("Method Not Allowed", { status: 405 });
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,6 +43,7 @@ export function staticFiles<T>(): MiddlewareFn<T> {
|
|||||||
if (cacheKey !== null && BUILD_ID !== cacheKey) {
|
if (cacheKey !== null && BUILD_ID !== cacheKey) {
|
||||||
url.searchParams.delete(ASSET_CACHE_BUST_KEY);
|
url.searchParams.delete(ASSET_CACHE_BUST_KEY);
|
||||||
const location = url.pathname + url.search;
|
const location = url.pathname + url.search;
|
||||||
|
file.close();
|
||||||
return new Response(null, {
|
return new Response(null, {
|
||||||
status: 307,
|
status: 307,
|
||||||
headers: {
|
headers: {
|
||||||
@@ -67,15 +69,19 @@ export function staticFiles<T>(): MiddlewareFn<T> {
|
|||||||
} else {
|
} else {
|
||||||
const ifNoneMatch = req.headers.get("If-None-Match");
|
const ifNoneMatch = req.headers.get("If-None-Match");
|
||||||
if (
|
if (
|
||||||
etag !== null &&
|
ifNoneMatch !== null &&
|
||||||
(ifNoneMatch === etag || ifNoneMatch === "W/" + etag)
|
(ifNoneMatch === etag || ifNoneMatch === `W/"${etag}"`)
|
||||||
) {
|
) {
|
||||||
|
file.close();
|
||||||
return new Response(null, { status: 304, headers });
|
return new Response(null, { status: 304, headers });
|
||||||
|
} else if (etag !== null) {
|
||||||
|
headers.set("Etag", `W/"${etag}"`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
headers.set("Content-Length", String(file.size));
|
headers.set("Content-Length", String(file.size));
|
||||||
if (req.method === "HEAD") {
|
if (req.method === "HEAD") {
|
||||||
|
file.close();
|
||||||
return new Response(null, { status: 200, headers });
|
return new Response(null, { status: 200, headers });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ class MockBuildCache implements BuildCache {
|
|||||||
hash: info.hash,
|
hash: info.hash,
|
||||||
size: text.byteLength,
|
size: text.byteLength,
|
||||||
readable: text,
|
readable: text,
|
||||||
|
close: () => {},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -79,18 +80,22 @@ Deno.test("static files - etag", async () => {
|
|||||||
{ buildCache },
|
{ buildCache },
|
||||||
);
|
);
|
||||||
|
|
||||||
const headers = new Headers();
|
|
||||||
headers.append("If-None-Match", "123");
|
|
||||||
const cacheUrl = `/foo.css?${ASSET_CACHE_BUST_KEY}=${BUILD_ID}`;
|
const cacheUrl = `/foo.css?${ASSET_CACHE_BUST_KEY}=${BUILD_ID}`;
|
||||||
const res = await server.get(cacheUrl, { headers });
|
let res = await server.get(cacheUrl);
|
||||||
|
await res.body?.cancel();
|
||||||
|
expect(res.headers.get("Etag")).toEqual('W/"123"');
|
||||||
|
|
||||||
|
let headers = new Headers();
|
||||||
|
headers.append("If-None-Match", "123");
|
||||||
|
res = await server.get(cacheUrl, { headers });
|
||||||
await res.body?.cancel();
|
await res.body?.cancel();
|
||||||
expect(res.status).toEqual(304);
|
expect(res.status).toEqual(304);
|
||||||
|
|
||||||
const headers2 = new Headers();
|
headers = new Headers();
|
||||||
headers2.append("If-None-Match", "W/123");
|
headers.append("If-None-Match", 'W/"123"');
|
||||||
const res2 = await server.get(cacheUrl, { headers });
|
res = await server.get(cacheUrl, { headers });
|
||||||
await res2.body?.cancel();
|
await res.body?.cancel();
|
||||||
expect(res2.status).toEqual(304);
|
expect(res.status).toEqual(304);
|
||||||
});
|
});
|
||||||
|
|
||||||
Deno.test("static files - 404 on missing favicon.ico", async () => {
|
Deno.test("static files - 404 on missing favicon.ico", async () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user