52 lines
1.3 KiB
JavaScript

const CACHE_NAME = 'toadstool-tally-v2';
const ASSETS = [
'/',
'/index.html',
'/assets/style.css',
'/assets/app.js?v=3',
'/assets/textures/mushroom.svg',
'/assets/icons/favicon-96x96.png',
'/assets/icons/favicon.ico',
'/assets/icons/favicon.svg',
'/assets/icons/apple-touch-icon.png',
'/assets/icons/web-app-manifest-192x192.png',
'/assets/icons/web-app-manifest-512x512.png',
'/assets/site.webmanifest'
];
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => cache.addAll(ASSETS))
);
self.skipWaiting();
});
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((keys) =>
Promise.all(
keys
.filter((key) => key !== CACHE_NAME)
.map((key) => caches.delete(key))
)
)
);
self.clients.claim();
});
self.addEventListener('fetch', (event) => {
const { request } = event;
if (request.method !== 'GET') return;
event.respondWith(
caches.match(request).then((cached) => {
if (cached) return cached;
return fetch(request).then((response) => {
const copy = response.clone();
caches.open(CACHE_NAME).then((cache) => cache.put(request, copy));
return response;
}).catch(() => cached);
})
);
});