-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.js
72 lines (42 loc) · 1.36 KB
/
worker.js
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
/****************************************************************************
* worker.js
* openacousticdevices.info
* December 2022
*****************************************************************************/
/* global self, caches */
const cacheName = 'audiomothchime-v1';
self.addEventListener('install', (e) => {
console.log('Caching assets for offline use');
e.waitUntil((async () => {
const cache = await caches.open(cacheName);
await cache.addAll(['./assets/favicon.png',
'./index.css',
'./index.html',
'./scripts/index.js',
'./scripts/audiomothchime_connector.js',
'./scripts/audiomothchime.js'
]);
})());
});
self.addEventListener('fetch', (e) => {
e.respondWith((async () => {
const r = await caches.match(e.request);
if (r) {
return r;
}
const response = await fetch(e.request);
const cache = await caches.open(cacheName);
cache.put(e.request, response.clone());
return response;
})());
});
self.addEventListener('activate', (e) => {
e.waitUntil(caches.keys().then((keyList) => {
return Promise.all(keyList.map((key) => {
if (key === cacheName) {
return 0;
}
return caches.delete(key);
}));
}));
});