forked from Qlugat/qlugat.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
56 lines (48 loc) · 1.54 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
52
53
54
55
56
const filesToCache = ["/app-A7GJVIB4.js","/favicon.ico","/img/demo.gif","/img/icon/arrow.svg","/img/icon/chevron.svg","/img/icon/hamburger.svg","/img/icon/rescript_256x256.png","/img/icon/rescript_512x512.png","/img/rescript.svg","/img/start.png","/","/manifest.json"];
const currentCacheName = "rescript-react-boilerplate-aa50ccaf5a0c0872d5c9fd0917fbad4cff1236cca4cb37b81e971f78ba00fc5c";
self.addEventListener("install", (event) => {
event.waitUntil(
caches.open(currentCacheName).then((cache) => cache.addAll(filesToCache))
);
});
self.addEventListener("fetch", (event) => {
event.respondWith(
(async () => {
const cachedResponse = await caches.match(event.request);
if (cachedResponse) {
return cachedResponse;
}
try {
return await fetch(event.request);
} catch (error) {
if (event.request.mode === "navigate") {
return caches.match("/");
}
throw error;
}
})()
);
});
addEventListener("activate", (event) => {
event.waitUntil(
(async () =>
caches
.keys()
.then((cacheNames) =>
Promise.all(
cacheNames
.filter(
(cacheName) =>
cacheName.startsWith("rescript-react-boilerplate") &&
cacheName !== currentCacheName
)
.map((cacheName) => caches.delete(cacheName))
)
))()
);
});
self.addEventListener("message", (event) => {
if (event.data.action === "skipWaiting") {
self.skipWaiting();
}
});