fix: Service worker activate immediately
This commit is contained in:
@@ -1,21 +1,43 @@
|
|||||||
const CACHE_NAME = 'pascar';
|
const CACHE_NAME = 'pascar-v1';
|
||||||
const ASSETS = [
|
const ASSETS = [
|
||||||
|
'./',
|
||||||
'index.html',
|
'index.html',
|
||||||
'manifest.json'
|
'manifest.json'
|
||||||
];
|
];
|
||||||
|
|
||||||
// Install: Cache the files
|
|
||||||
self.addEventListener('install', (e) => {
|
self.addEventListener('install', (e) => {
|
||||||
|
self.skipWaiting();
|
||||||
e.waitUntil(
|
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);
|
||||||
|
})
|
||||||
// Fetch: Serve from cache if offline
|
);
|
||||||
self.addEventListener('fetch', (e) => {
|
});
|
||||||
e.respondWith(
|
|
||||||
caches.match(e.request).then((response) => {
|
self.addEventListener('activate', (e) => {
|
||||||
return response || fetch(e.request);
|
e.waitUntil(
|
||||||
|
Promise.all([
|
||||||
|
clients.claim(),
|
||||||
|
caches.keys().then((keys) => {
|
||||||
|
return Promise.all(
|
||||||
|
keys.filter(key => key !== CACHE_NAME).map(key => caches.delete(key))
|
||||||
|
);
|
||||||
|
})
|
||||||
|
])
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
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');
|
||||||
|
}
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
Reference in New Issue
Block a user