48 lines
1.1 KiB
JavaScript
48 lines
1.1 KiB
JavaScript
const CACHE_NAME = 'recreopay-static-v1';
|
|
const ASSETS_TO_CACHE = [
|
|
'/',
|
|
'/index.html',
|
|
'/pos.html',
|
|
'/carnet.html',
|
|
'/css/styles.css',
|
|
'/js/sounds.js',
|
|
'/manifest.json'
|
|
];
|
|
|
|
self.addEventListener('install', (event) => {
|
|
event.waitUntil(
|
|
caches.open(CACHE_NAME).then((cache) => {
|
|
return cache.addAll(ASSETS_TO_CACHE).catch(err => console.log('Cache warn:', err));
|
|
})
|
|
);
|
|
self.skipWaiting();
|
|
});
|
|
|
|
self.addEventListener('activate', (event) => {
|
|
event.waitUntil(
|
|
caches.keys().then((keys) => {
|
|
return Promise.all(
|
|
keys.filter((key) => key !== CACHE_NAME).map((key) => caches.delete(key))
|
|
);
|
|
})
|
|
);
|
|
self.clients.claim();
|
|
});
|
|
|
|
self.addEventListener('fetch', (event) => {
|
|
// Let API requests pass through to the network
|
|
if (event.request.url.includes('/api/')) {
|
|
return;
|
|
}
|
|
|
|
event.respondWith(
|
|
caches.match(event.request).then((cachedResponse) => {
|
|
return cachedResponse || fetch(event.request).catch(() => {
|
|
if (event.request.mode === 'navigate') {
|
|
return caches.match('/index.html');
|
|
}
|
|
});
|
|
})
|
|
);
|
|
});
|