From 246cdb4af0a93de7618b099aa12f96ebd962c1ad Mon Sep 17 00:00:00 2001 From: DrMint <29893320+DrMint@users.noreply.github.com> Date: Sat, 29 Jun 2024 13:56:46 +0200 Subject: [PATCH] Fixed If modfied since logic --- src/middleware/pageCaching.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/middleware/pageCaching.ts b/src/middleware/pageCaching.ts index ce21bca..78f7e66 100644 --- a/src/middleware/pageCaching.ts +++ b/src/middleware/pageCaching.ts @@ -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" }); }