-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathservice-worker.js
More file actions
54 lines (50 loc) · 1.3 KB
/
service-worker.js
File metadata and controls
54 lines (50 loc) · 1.3 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
const cacheName = 'recoder-v1';
const filesToCache = [
'/',
'/index.html',
'/recoder.js',
'/recoder_bg.wasm',
'/pkg/recoder.js',
'/pkg/recoder_bg.wasm',
];
self.addEventListener("install", (e) => {
console.debug("[Service Worker] Install");
e.waitUntil(
(async () => {
const cache = await caches.open(cacheName);
console.debug("[Service Worker] Caching all: app shell and content");
await cache.addAll(filesToCache);
})()
);
});
self.addEventListener("activate", (e) => {
console.debug("[Service Worker] Clearing old cache");
e.waitUntil(
caches.keys().then((keyList) => {
return Promise.all(
keyList.map((key) => {
if (key === cacheName) {
return;
}
return caches.delete(key);
})
);
})
);
});
self.addEventListener("fetch", (e) => {
e.respondWith(
(async () => {
const r = await caches.match(e.request);
console.debug(`[Service Worker] Fetching resource: ${e.request.url}`);
if (r) {
return r;
}
const response = await fetch(e.request);
const cache = await caches.open(cacheName);
console.debug(`[Service Worker] Caching new resource: ${e.request.url}`);
cache.put(e.request, response.clone());
return response;
})()
);
});