From 3f22ccb91b5f03c9e4ba300ef8629550b6003b9c Mon Sep 17 00:00:00 2001 From: Nirmal Kumar R Date: Sun, 31 May 2026 00:43:25 +0530 Subject: [PATCH] fix: Service worker activate immediately --- sw.js | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/sw.js b/sw.js index e23f278..e438fa8 100644 --- a/sw.js +++ b/sw.js @@ -1,21 +1,43 @@ -const CACHE_NAME = 'pascar'; +const CACHE_NAME = 'pascar-v1'; const ASSETS = [ + './', 'index.html', 'manifest.json' ]; -// Install: Cache the files self.addEventListener('install', (e) => { + self.skipWaiting(); e.waitUntil( - caches.open(CACHE_NAME).then((cache) => cache.addAll(ASSETS)) + caches.open(CACHE_NAME).then((cache) => { + console.log('Caching assets'); + return cache.addAll(ASSETS); + }) + ); +}); + +self.addEventListener('activate', (e) => { + e.waitUntil( + Promise.all([ + clients.claim(), + caches.keys().then((keys) => { + return Promise.all( + keys.filter(key => key !== CACHE_NAME).map(key => caches.delete(key)) + ); + }) + ]) ); }); -// Fetch: Serve from cache if offline self.addEventListener('fetch', (e) => { e.respondWith( caches.match(e.request).then((response) => { + // Return cached version OR fetch from network return response || fetch(e.request); + }).catch(() => { + // Fallback for when both cache and network fail + if (e.request.mode === 'navigate') { + return caches.match('index.html'); + } }) ); }); \ No newline at end of file