-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
16 lines (15 loc) · 1.05 KB
/
sw.js
File metadata and controls
16 lines (15 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
const VERSION='pwa-v3.1';
const CACHE='depthlogger-'+VERSION;
// update to new html name
const ASSETS=['./','./index_pwa_v3.html','./manifest.json','./icon-192.png','./icon-512.png','./brownfield_logo.png'];
self.addEventListener('install',e=>{e.waitUntil(caches.open(CACHE).then(c=>c.addAll(ASSETS))); self.skipWaiting();});
self.addEventListener('activate',e=>{e.waitUntil(caches.keys().then(keys=>Promise.all(keys.map(k=>k===CACHE?null:caches.delete(k))))); self.clients.claim();});
self.addEventListener('fetch',e=>{
if(e.request.method!=='GET') return;
e.respondWith((async()=>{
const cached = await caches.match(e.request);
if(cached){ e.waitUntil(fetch(e.request).then(r=>caches.open(CACHE).then(c=>c.put(e.request,r.clone()))).catch(()=>{})); return cached; }
try{ const fresh = await fetch(e.request); const c = await caches.open(CACHE); c.put(e.request,fresh.clone()); return fresh; }
catch(err){ if(e.request.headers.get('accept')?.includes('text/html')) return caches.match('./index_pwa_v3.html'); throw err; }
})());
});