-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsw.js
More file actions
27 lines (26 loc) · 1014 Bytes
/
Copy pathsw.js
File metadata and controls
27 lines (26 loc) · 1014 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// picoTracker Librarian service worker — cache-first with background
// refresh, so the app opens instantly offline and still picks up
// deployed updates on the next visit.
const CACHE = 'pt-librarian-v1-5-2';
const ASSETS = ['./', 'index.html', 'manifest.webmanifest', 'icon-192.png', 'icon-512.png'];
self.addEventListener('install', e => {
e.waitUntil(caches.open(CACHE).then(c => c.addAll(ASSETS)).catch(() => {}));
self.skipWaiting();
});
self.addEventListener('activate', e => {
e.waitUntil(caches.keys().then(keys =>
Promise.all(keys.filter(k => k !== CACHE).map(k => caches.delete(k)))));
self.clients.claim();
});
self.addEventListener('fetch', e => {
if (e.request.method !== 'GET') return;
e.respondWith(
caches.match(e.request).then(cached => {
const refresh = fetch(e.request).then(res => {
if (res.ok) caches.open(CACHE).then(c => c.put(e.request, res.clone()));
return res;
}).catch(() => cached);
return cached || refresh;
})
);
});