-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
executable file
·47 lines (42 loc) · 1.07 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
const CACHE_ID="v1"
const addResourcesToCache = async (resources) => {
const cache = await caches.open(CACHE_ID);
await cache.addAll(resources);
};
self.addEventListener("install", (event) => {
event.waitUntil(
addResourcesToCache([
'./',
'./index.html'
])
);
});
self.addEventListener('activate', (event) => {
self.clients.claim();
event.waitUntil(
(async () => {
if ('navigationPreload' in self.registration) {
await self.registration.navigationPreload.enable();
}
})(),
);
});
self.addEventListener('fetch', (event) => {
event.respondWith(
(async () => {
try {
const response = await event.preloadResponse;
if (response) {
return response;
}
return fetch(event.request);
} catch {
const cache = await caches.open(CACHE_ID)
cache.match(event.request).then(async response => {
if (response) return response; // get cached
else new Response(`could not find ${event.request} in cache`);
})
}
})(),
);
});