-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsw.js
51 lines (45 loc) · 1.43 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
41
42
43
44
45
46
47
48
49
50
51
const cacheName = "CocoNutsScouting_2023-10-20_2";
const precacheResources = [
"/CocoNuts-Scouting/",
"/CocoNuts-Scouting/index.html",
"/CocoNuts-Scouting/assets/qrcode.min.js",
"/CocoNuts-Scouting/assets/bootstrap.min.css",
"/CocoNuts-Scouting/assets/bootstrap.min.css.map",
"/CocoNuts-Scouting/assets/jquery-3.6.1.min.js"
];
if ("serviceWorker" in navigator) {
navigator.serviceWorker
.register("sw.js")
.then(() => console.log("Successfully registered service worker"));
}
self.addEventListener('install', (event) => {
event.waitUntil((async () => {
const cache = await caches.open(cacheName);
await cache.addAll(precacheResources);
})());
});
self.addEventListener('fetch', (event) => {
event.respondWith((async () => {
// try the cache first
const r = await caches.match(event.request);
if (r) { return r; }
// cache the new resource and return it
const response = await fetch(event.request);
const cache = await caches.open(cacheName);
cache.put(event.request, response.clone());
return response;
})());
});
self.addEventListener('activate', event => {
event.waitUntil(
caches.keys().then(cacheList => {
return Promise.all(
cacheList.filter(thisCache => {
return thisCache.startsWith('CocoNutsScouting_') && thisCache !== cacheName;
}).map(thisCache => {
return caches.delete(thisCache);
})
);
})
);
});