-
Notifications
You must be signed in to change notification settings - Fork 4
/
sw.js
39 lines (33 loc) · 1.03 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
self.addEventListener("activate", event => {
event.waitUntil(self.clients.claim());
});
self.addEventListener("install", (event) => {
event.waitUntil(self.skipWaiting());
});
self.addEventListener("fetch", async (event) => {
if (event.request.method !== "GET" ||
(event.request.mode !== "navigate" &&
event.request.destination !== "script")) {
return; // cache only index page and scripts
}
event.respondWith((async () => {
let response;
let responseError;
try {
response = await fetch(event.request);
} catch (e) {
responseError = e;
}
if (response) {
(await caches.open("demo")).put(event.request, response.clone());
return response;
}
response = await caches.match(event.request);
if (response) {
console.log("loaded from SW cache", event.request.url);
return response;
} else {
throw responseError;
}
})());
});