Fixed If modfied since logic

This commit is contained in:
DrMint 2024-06-29 13:56:46 +02:00
parent cbbf0e5e3b
commit 246cdb4af0
1 changed files with 7 additions and 3 deletions

View File

@ -8,10 +8,14 @@ export const pageCachingMiddleware = defineMiddleware(async ({ url, request, loc
const cachedPage = pageCache.get(pathname);
if (cachedPage) {
const modifiedSince = request.headers.get("If-Modified-Since");
const lastModified = cachedPage.headers.get("Last-Modified");
const clientTimestamp = request.headers.get("If-Modified-Since");
const serverTimestamp = cachedPage.headers.get("Last-Modified");
if (modifiedSince && lastModified && new Date(modifiedSince) <= new Date(lastModified)) {
if (
clientTimestamp &&
serverTimestamp &&
new Date(clientTimestamp) == new Date(serverTimestamp)
) {
return new Response(null, { status: 304, statusText: "Not Modified" });
}