-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
75 lines (68 loc) · 2.1 KB
/
Copy pathsw.js
File metadata and controls
75 lines (68 loc) · 2.1 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
/* Ledger — Service Worker for PWA install on Android */
const CACHE_VERSION = 'ledger-v2';
const CORE_CACHE = CACHE_VERSION + '-core';
const RUNTIME_CACHE = CACHE_VERSION + '-runtime';
const CORE_ASSETS = [
'./',
'./index.html',
'./styles.css',
'./site.js',
'./manifest.json',
'./favicon.svg',
'./favicon.ico',
'./favicon-96.png',
'./favicon-48.png',
'./favicon-192.png',
'./favicon-512.png',
'./og-image.svg',
'./Ledger-logo.svg'
];
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CORE_CACHE).then((cache) => {
return cache.addAll(CORE_ASSETS.map(u => new Request(u, {cache: 'reload'})));
}).then(() => self.skipWaiting())
);
});
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((keys) =>
Promise.all(keys.filter(k => !k.startsWith(CACHE_VERSION)).map(k => caches.delete(k)))
).then(() => self.clients.claim())
);
});
self.addEventListener('fetch', (event) => {
const req = event.request;
if (req.method !== 'GET') return;
const url = new URL(req.url);
if (url.origin !== self.location.origin) return;
// Navigation: network first, fallback to cache
if (req.mode === 'navigate' || req.headers.get('accept')?.includes('text/html')) {
event.respondWith(
fetch(req).then((res) => {
if (res && res.ok) {
const copy = res.clone();
caches.open(RUNTIME_CACHE).then(c => c.put(req, copy));
}
return res;
}).catch(() => caches.match(req).then(r => r || caches.match('./index.html').then(r2 => r2 || caches.match('./'))))
);
return;
}
// Static assets: cache first, refresh in background
event.respondWith(
caches.match(req).then((cached) => {
const fetched = fetch(req).then((res) => {
if (res && res.ok) {
const copy = res.clone();
caches.open(RUNTIME_CACHE).then(c => c.put(req, copy));
}
return res;
}).catch(() => null);
return cached || fetched;
})
);
});
self.addEventListener('message', (event) => {
if (event.data === 'skipWaiting') self.skipWaiting();
});