-
Notifications
You must be signed in to change notification settings - Fork 0
/
serviceWorker.js
40 lines (35 loc) · 1.15 KB
/
serviceWorker.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
if ('serviceWorker' in navigator) {
window.addEventListener("load", () => {
navigator.serviceWorker.register('/serviceWorker.js')
.then(reg => console.log("Service worker registered"))
.catch(err => console.error(`Service Worker Error: ${err}`));
});
}
else {
console.log("Service Worker is not supported by browser.");
}
const cacheName = "calculator";
const cachedAssets = ["/", "/index.html", "/src/master.css", "/src/index.js", "/manifest.json"];
self.addEventListener("install", (e) => {
console.log('Service worker installed')
e.waitUntil(
caches
.open(cacheName)
.then((cache) => { cache.addAll(cachedAssets); })
.then(() => self.skipWaiting())
)
})
self.addEventListener("activate", (e) => {
e.waitUntil(
caches.keys().then(cacheNames => {
return Promise.all(cacheNames.map(cache => {
if (cache !== cacheName) {
return caches.delete(cache);
}
}));
}));
});
self.addEventListener("fetch", (e) => {
console.log("Fetching data from cache")
e.respondWith(fetch(e.request).catch(() => caches.match(e.request)));
});