-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
40 lines (37 loc) · 1.3 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
let cacheName = "enzcalc-2020090400";
let filesToCache = [
"/enzyme-calculator/",
"index.html",
"enzicon192.png",
"https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css",
"https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.slim.min.js",
"https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/js/bootstrap.min.js"
];
// On install of a SW, cache all required resources
self.addEventListener("install", e => {
e.waitUntil(
caches.open(cacheName).then(cache => {
console.log("Installing cache", cacheName);
return cache.addAll(filesToCache);
})
);
self.skipWaiting();
});
// On activation of a SW, remove all old caches then claim clients
self.addEventListener("activate", e => {
e.waitUntil(
caches.keys().then(keyList => Promise.all(keyList.map(key => {
if (key !== cacheName) {
console.log("Removing cache", key);
return caches.delete(key);
}
})))
);
self.clients.claim();
});
// Respond with cached files first, falling back to fetch
self.addEventListener("fetch", e => {
e.respondWith(
caches.open(cacheName).then(cache => cache.match(e.request).then(response => response || fetch(e.request)))
);
});