-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice-worker.js
More file actions
61 lines (56 loc) · 1.83 KB
/
service-worker.js
File metadata and controls
61 lines (56 loc) · 1.83 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
const version = "resound-004";
// paths resolved relative to the service worker file location
const assets = [
'/',
'package.html',
'img/icon.png',
'img/sphere-down.png',
'img/sphere-up.png'
].map(p => new URL(p, self.location).toString());
self.addEventListener('install', event => {
event.waitUntil((async () => {
console.log('@install');
const cache = await caches.open(version);
try {
await cache.addAll(assets);
console.log('cached assets', assets);
} catch (err) {
console.warn('cache.addAll failed (some assets may be missing)', err);
// continue install even if some assets fail
}
await self.skipWaiting();
})());
});
self.addEventListener('activate', event => {
event.waitUntil((async () => {
console.log('@activate');
const keys = await caches.keys();
await Promise.all(keys.map(key => key !== version ? caches.delete(key) : Promise.resolve()));
await self.clients.claim();
})());
});
self.addEventListener('fetch', event => {
event.respondWith(performFetch(event));
});
async function performFetch(event) {
const request = event.request;
try {
const cached = await caches.match(request);
if (cached) return cached;
const networkResponse = await fetch(request);
// optionally cache GET responses so next time we can serve from cache
if (request.method === 'GET' && networkResponse && networkResponse.ok) {
const c = await caches.open(version);
c.put(request, networkResponse.clone());
}
return networkResponse;
} catch (err) {
// navigation fallback: return cached package.html if available
if (request.mode === 'navigate') {
const fallback = await caches.match(new URL('package.html', self.location).toString());
if (fallback) return fallback;
}
// rethrow so devtools show the error
throw err;
}
}