-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserviceworker.js
46 lines (40 loc) · 1.31 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
self.addEventListener('install', function(e) {
e.waitUntil(
caches.open('v1.3').then(function(cache) {
return cache.addAll([
'/',
'/index.html',
'/scripts/game.js',
'/css/style.css',
'/assets/icons/hamburger_icon.svg',
'/assets/favicons/favicon.ico',
'/assets/favicons/android-chrome-192x192.png',
'/assets/favicons/android-chrome-512x512.png',
'/assets/favicons/apple-touch-icon.png',
'/assets/favicons/favicon-16x16.png',
'/assets/favicons/favicon-32x32.png',
'/site.webmanifest'
]);
})
);
});
self.addEventListener('fetch', function(event) {
console.log(event.request.url);
event.respondWith(
caches.match(event.request).then(function(response) {
return response || fetch(event.request);
})
);
});
const deleteCache = async (key) => {
await caches.delete(key);
};
const deleteOldCaches = async () => {
const cacheKeepList = ["v1.3"];
const keyList = await caches.keys();
const cachesToDelete = keyList.filter((key) => !cacheKeepList.includes(key));
await Promise.all(cachesToDelete.map(deleteCache));
};
self.addEventListener("activate", (event) => {
event.waitUntil(deleteOldCaches());
});