From 5c1981374cdf0e3d2deeee375c8c1ee1b7a9e1c4 Mon Sep 17 00:00:00 2001 From: Aryan Kumar Date: Thu, 12 Feb 2026 23:57:13 +0530 Subject: [PATCH] Enhance caching strategy in service worker and update Vercel configuration to include Cache-Control headers for index.html and root path --- frontend/public/service-worker.js | 18 +++++++++++++----- frontend/vercel.json | 6 ++++-- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/frontend/public/service-worker.js b/frontend/public/service-worker.js index 4e15aee..1181a41 100644 --- a/frontend/public/service-worker.js +++ b/frontend/public/service-worker.js @@ -4,7 +4,7 @@ */ // Cache versioning - increment to invalidate all caches -const CACHE_VERSION = 3; +const CACHE_VERSION = 4; const CACHE_PREFIX = "know-india"; const CACHE_NAME = `${CACHE_PREFIX}-cache-v${CACHE_VERSION}`; @@ -12,10 +12,8 @@ const IMAGE_CACHE_NAME = `${CACHE_PREFIX}-images-v${CACHE_VERSION}`; const API_CACHE_NAME = `${CACHE_PREFIX}-api-v${CACHE_VERSION}`; const CURRENT_CACHES = [CACHE_NAME, IMAGE_CACHE_NAME, API_CACHE_NAME]; -// Static assets to cache on install +// Static assets to cache on install (exclude / and index.html so deploys always get fresh HTML) const STATIC_ASSETS = [ - "/", - "/index.html", "/offline.html", "/manifest.json", "/logo192.png", @@ -111,7 +109,17 @@ self.addEventListener("fetch", (event) => { return; } - // Other requests - network first + // HTML / navigation - network only, never cache (avoids white page after deploy) + if (isNavigationRequest(request) || url.pathname === "/" || url.pathname === "/index.html") { + event.respondWith( + fetch(request) + .then((response) => response) + .catch(() => caches.match("/offline.html")) + ); + return; + } + + // Other requests (JS, CSS, etc.) - network first, then cache event.respondWith( fetch(request) .then((response) => { diff --git a/frontend/vercel.json b/frontend/vercel.json index eaba449..9174554 100644 --- a/frontend/vercel.json +++ b/frontend/vercel.json @@ -2,7 +2,9 @@ "buildCommand": "npm run build", "outputDirectory": "build", "framework": "create-react-app", - "rewrites": [ - { "source": "/(.*)", "destination": "/index.html" } + "rewrites": [{ "source": "/(.*)", "destination": "/index.html" }], + "headers": [ + { "source": "/index.html", "headers": [{ "key": "Cache-Control", "value": "no-cache, no-store, must-revalidate" }] }, + { "source": "/", "headers": [{ "key": "Cache-Control", "value": "no-cache, no-store, must-revalidate" }] } ] }