-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserviceworker.js
57 lines (54 loc) · 1.45 KB
/
serviceworker.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
const CACHE_VERSION = 'v1.2.2'
const CURRENT_CACHES = {
'polyrhythm-calculator': 'polyrhythm-calculator-' + CACHE_VERSION,
}
self.addEventListener('install', function (e) {
e.waitUntil(
caches
.open(CURRENT_CACHES['polyrhythm-calculator'])
.then((cache) =>
cache.addAll([
'.',
'polyrhythm-calculator.js',
'images/background.webp',
'images/favicon.svg',
'images/favicon512x512.png',
'images/favicon192x192.png',
'manifest.json',
])
)
)
})
self.addEventListener('activate', function (event) {
const expectedCacheNamesSet = new Set(Object.values(CURRENT_CACHES))
event.waitUntil(
caches.keys().then(function (cacheNames) {
return Promise.all(
cacheNames.map(function (cacheName) {
if (!expectedCacheNamesSet.has(cacheName)) {
return caches.delete(cacheName)
}
})
)
})
)
})
self.addEventListener('fetch', (event) => {
event.respondWith(
caches
.open(CURRENT_CACHES['polyrhythm-calculator'])
.then((cache) =>
cache.match(event.request).then(
(response) =>
response ||
fetch(event.request.clone()).then((response) => {
if (response.status === 200) {
cache.put(event.request, response.clone())
}
return response
})
)
)
.catch(console.warn)
)
})