-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
76 lines (67 loc) · 2.03 KB
/
Copy pathsw.js
File metadata and controls
76 lines (67 loc) · 2.03 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
66
67
68
69
70
71
72
73
74
75
76
// Service worker. VERSION is rewritten by server.js on startup to bust the cache.
const VERSION = 'static-20260709-portrait-map';
const CACHE = 'nus-isb-' + VERSION;
const SHELL = [
'./',
'./index.html',
'./auth-callback/',
'./manifest.json',
'./icon.svg',
'./icon-192.png',
'./icon-512.png',
];
self.addEventListener('install', e => {
e.waitUntil((async () => {
const c = await caches.open(CACHE);
await Promise.all(SHELL.map(u => c.add(u).catch(() => null)));
self.skipWaiting();
})());
});
self.addEventListener('activate', e => {
e.waitUntil((async () => {
const keys = await caches.keys();
await Promise.all(keys.filter(k => k !== CACHE).map(k => caches.delete(k)));
await self.clients.claim();
})());
});
self.addEventListener('message', e => {
if (e.data === 'skip-waiting') self.skipWaiting();
});
self.addEventListener('fetch', e => {
const req = e.request;
if (req.method !== 'GET') return;
const url = new URL(req.url);
if (url.origin !== self.location.origin) return;
// Live API responses — never cache; let the network fail naturally when offline.
if (url.pathname.startsWith('/api/')) return;
// Network-first for HTML — users get fresh content; cache is offline fallback.
if (req.mode === 'navigate' || req.destination === 'document') {
e.respondWith((async () => {
try {
const res = await fetch(req);
const c = await caches.open(CACHE);
c.put(req, res.clone());
return res;
} catch {
const cached = await caches.match(req);
return cached || caches.match('./index.html');
}
})());
return;
}
// Cache-first for static assets.
e.respondWith((async () => {
const cached = await caches.match(req);
if (cached) return cached;
try {
const res = await fetch(req);
if (res && res.ok && res.type === 'basic') {
const c = await caches.open(CACHE);
c.put(req, res.clone());
}
return res;
} catch {
return cached || Response.error();
}
})());
});