Network-first service worker to clear stale caches

This commit is contained in:
Amirul Islam Redwan71
2026-09-01 09:23:31 +06:00
parent 31ab617740
commit 31fd8d3195
+4 -22
View File
@@ -1,19 +1,6 @@
const CACHE_NAME = 'opal-ai-v2-cache';
const ASSETS = [
'./',
'./index.html',
'./style.css',
'./app.js',
'./manifest.json',
'https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js'
];
const CACHE_NAME = 'opal-ai-v5-cache';
self.addEventListener('install', (e) => {
e.waitUntil(
caches.open(CACHE_NAME).then((cache) => {
return cache.addAll(ASSETS);
})
);
self.skipWaiting();
});
@@ -21,19 +8,14 @@ self.addEventListener('activate', (e) => {
e.waitUntil(
caches.keys().then((keys) => {
return Promise.all(
keys.map((key) => {
if (key !== CACHE_NAME) return caches.delete(key);
})
keys.map((key) => caches.delete(key))
);
})
}).then(() => self.clients.claim())
);
self.clients.claim();
});
self.addEventListener('fetch', (e) => {
e.respondWith(
caches.match(e.request).then((res) => {
return res || fetch(e.request);
})
fetch(e.request).catch(() => caches.match(e.request))
);
});