-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
65 lines (59 loc) · 1.92 KB
/
sw.js
File metadata and controls
65 lines (59 loc) · 1.92 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Offline shell cache + network-first JSON cache
const SHELL_CACHE = 'sb-shell-v4';
const DATA_CACHE = 'sb-data-v2';
const SHELL = [
'/', '/index.html',
'/assets/styles.css',
'/src/app.js', '/src/data.js', '/src/search-worker.js',
'/manifest.webmanifest', '/favicon.svg'
];
self.addEventListener('install', (e)=>{
e.waitUntil((async()=>{
const c = await caches.open(SHELL_CACHE);
try { await c.addAll(SHELL); } catch(_) { /* ok when hosted under a subpath */ }
self.skipWaiting();
})());
});
self.addEventListener('activate', (e)=>{
e.waitUntil((async()=>{
const keep = new Set([SHELL_CACHE, DATA_CACHE]);
for (const k of await caches.keys()){
if(!keep.has(k)) await caches.delete(k);
}
self.clients.claim();
})());
});
self.addEventListener('fetch', (e)=>{
const url = new URL(e.request.url);
if(url.origin !== location.origin) return;
// JSON: network-first so new data wins without hard reload
if(url.pathname.endsWith('.json')){
e.respondWith((async()=>{
const cache = await caches.open(DATA_CACHE);
try{
const fresh = await fetch(e.request, { cache: 'no-store' });
if (fresh.ok) cache.put(e.request, fresh.clone());
return fresh;
}catch{
const cached = await cache.match(e.request);
return cached || new Response('Offline', { status: 503, statusText: 'Offline' });
}
})());
return;
}
// Everything else: cache-first (fast shell)
e.respondWith((async()=>{
const cache = await caches.open(SHELL_CACHE);
const cached = await cache.match(e.request);
if(cached) return cached;
try{
const res = await fetch(e.request);
if(res.ok && (url.pathname.startsWith('/assets/') || url.pathname.startsWith('/src/'))){
cache.put(e.request, res.clone());
}
return res;
}catch{
return cached || new Response('Offline', { status: 503, statusText: 'Offline' });
}
})());
});