-
Notifications
You must be signed in to change notification settings - Fork 0
/
service-worker.js
64 lines (59 loc) · 1.49 KB
/
service-worker.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
57
58
59
60
61
62
63
64
const cacheName = "v0.1.5";
const cacheAssets = [
"./",
"./index.html",
"./robots.txt",
"./static/css/style.css",
"./static/js/script.js",
"./static/img/admonition_icons/tip.png",
"./static/img/admonition_icons/warning.png",
"./static/img/admonition_icons/note.png",
"./static/img/admonition_icons/caution.png",
"./static/img/admonition_icons/important.png",
"./static/img/clear_cross.svg",
"./static/img/favicon.ico",
"./static/img/icon-192.png",
"./static/img/icon-512.png",
"./static/img/moon.svg",
"./static/img/sun.svg",
"./static/img/up-arrow.svg"
];
self.addEventListener("install", (e) => {
// Cache files
e.waitUntil(
caches
.open(cacheName)
.then((cache) => cache.addAll(cacheAssets))
.then(() => self.skipWaiting())
.catch((err) => console.error(`Cache error: ${err}`))
);
});
self.addEventListener("activate", (e) => {
e.waitUntil(
// Delete any previous cache
caches.keys().then((cacheKeys) => {
return Promise.all(
cacheKeys.map((cacheKey) => {
if (cacheKey !== cacheName) return caches.delete(cacheKey);
})
);
})
);
});
self.addEventListener("fetch", (e) => {
e.respondWith(
fetch(e.request)
.then((response) => {
const resClone = response.clone();
// Add response to cache
if (e.request.url.indexOf("http") === 0)
caches
.open(cacheName)
.then((cache) => cache.put(e.request, resClone));
return response;
})
.catch((err) =>
caches.match(e.request).then((response) => response)
)
);
});