-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_serviceWorker.js
42 lines (38 loc) · 945 Bytes
/
_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
const AIWAN_GAME_CACHE = "aiwan-game-cache-" + "$VERSION$";
const cache_url = "$CACHE_URL$";
// install
self.addEventListener("install", (event) => {
event.waitUntil(
caches
.open(AIWAN_GAME_CACHE)
.then((cache) => cache.addAll(cache_url))
.then(() => self.skipWaiting())
);
});
// activate
self.addEventListener("activate", (event) => {
event.waitUntil(
caches
.keys()
.then((keys) => {
return Promise.all(
keys.map((key) => {
if (key !== AIWAN_GAME_CACHE) {
return caches.delete(key);
}
})
);
})
.then(() => self.clients.claim())
);
});
// fetch
self.addEventListener("fetch", (event) => {
console.log(event.request.url);
event.respondWith(
caches
.open(AIWAN_GAME_CACHE)
.then((cache) => cache.match(event.request))
.then((response) => response || fetch(event.request))
);
});