Portsmouth Postal Scorer / sw.js
sw.js962 B
// Offline cache. Bump VERSION whenever you change any of the files below.
const VERSION = 'pp-v28';
const FILES = ['./', 'index.html', 'manifest.webmanifest', 'icon-192.png', 'icon-512.png', 'icon-maskable-512.png', 'logo.png'];
self.addEventListener('install', e => {
e.waitUntil(caches.open(VERSION).then(c => c.addAll(FILES)).then(() => self.skipWaiting()));
});
self.addEventListener('activate', e => {
e.waitUntil(caches.keys()
.then(keys => Promise.all(keys.filter(k => k !== VERSION).map(k => caches.delete(k))))
.then(() => self.clients.claim()));
});
self.addEventListener('fetch', e => {
if (e.request.method !== 'GET') return;
// network first so updates show up straight away; cache when offline at the range
e.respondWith(fetch(e.request).then(res => {
const copy = res.clone();
caches.open(VERSION).then(c => c.put(e.request, copy));
return res;
}).catch(() => caches.match(e.request, {ignoreSearch: true})));
});
