Files

43 lines
977 B
JavaScript

const CACHE_NAME = 'pascar';
const ASSETS = [
'./',
'index.html',
'manifest.json'
];
self.addEventListener('install', (e) => {
self.skipWaiting();
e.waitUntil(
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))
);
})
])
);
});
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');
}
})
);
});