Network-first service worker to clear stale caches
This commit is contained in:
@@ -1,19 +1,6 @@
|
|||||||
const CACHE_NAME = 'opal-ai-v2-cache';
|
const CACHE_NAME = 'opal-ai-v5-cache';
|
||||||
const ASSETS = [
|
|
||||||
'./',
|
|
||||||
'./index.html',
|
|
||||||
'./style.css',
|
|
||||||
'./app.js',
|
|
||||||
'./manifest.json',
|
|
||||||
'https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js'
|
|
||||||
];
|
|
||||||
|
|
||||||
self.addEventListener('install', (e) => {
|
self.addEventListener('install', (e) => {
|
||||||
e.waitUntil(
|
|
||||||
caches.open(CACHE_NAME).then((cache) => {
|
|
||||||
return cache.addAll(ASSETS);
|
|
||||||
})
|
|
||||||
);
|
|
||||||
self.skipWaiting();
|
self.skipWaiting();
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -21,19 +8,14 @@ self.addEventListener('activate', (e) => {
|
|||||||
e.waitUntil(
|
e.waitUntil(
|
||||||
caches.keys().then((keys) => {
|
caches.keys().then((keys) => {
|
||||||
return Promise.all(
|
return Promise.all(
|
||||||
keys.map((key) => {
|
keys.map((key) => caches.delete(key))
|
||||||
if (key !== CACHE_NAME) return caches.delete(key);
|
|
||||||
})
|
|
||||||
);
|
);
|
||||||
})
|
}).then(() => self.clients.claim())
|
||||||
);
|
);
|
||||||
self.clients.claim();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
self.addEventListener('fetch', (e) => {
|
self.addEventListener('fetch', (e) => {
|
||||||
e.respondWith(
|
e.respondWith(
|
||||||
caches.match(e.request).then((res) => {
|
fetch(e.request).catch(() => caches.match(e.request))
|
||||||
return res || fetch(e.request);
|
|
||||||
})
|
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user