-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsw.js
40 lines (37 loc) · 1.02 KB
/
sw.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
const cacheWhitelist = ['v1'];
const filesToCache = [
'/khrissbox/index.html',
'/khrissbox/main.js',
'/khrissbox/assets/css/main.css',
'/khrissbox/assets/sfx/allo/1.wav',
'/khrissbox/assets/sfx/allo/2.wav',
'/khrissbox/assets/sfx/allo/3.wav',
// '/khrissbox/assets/sfx/',
];
// On installation, add assets to cache
self.addEventListener('install', event => {
console.log('[Service worker] Install');
event.waitUntil(
caches.open('v1').then(cache => cache.addAll(filesToCache))
)
});
// Intercept requests to use cache when possible
self.addEventListener('fetch', event => {
console.log('[Service worker] Fetch');
event.respondWith(
caches.match(event.request).then(response => response || fetch(event.request))
)
});
// On update, remove old unused caches
self.addEventListener('activate', event => {
console.log('[Service worker] Activate');
event.waitUntil(
caches.keys().then(keys => Promise.all(
keys.map(key => {
if (!cacheWhitelist.includes(key)) {
return caches.delete(key);
}
})
))
)
});