-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoffline-uzitrake.js
executable file
·51 lines (42 loc) · 1.23 KB
/
offline-uzitrake.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
//custom serviceWorker
const cacheName = "uzitrake-offline";
// var cacheName = {
// offline: 'uzitrake-offline' + cacheVersion,
// }
let offlineUrl = "offline.html";
let offlineFont = "fonts/DiamondGrotesk.woff";
const cacheAssets = [offlineUrl, offlineFont];
//call install event
self.addEventListener("install", (e) => {
// console.log('Service-worker:installed');
e.waitUntil(
caches
.open(cacheName)
.then((cache) => {
// console.log('serviceWorker: caching files');
cache.addAll(cacheAssets);
})
.then(() => self.skipWaiting())
);
});
//call install event
self.addEventListener("activate", (e) => {
// console.log('Service-worker:activated');
e.waitUntil(
caches.keys().then((cacheNames) => {
return Promise.all(
cacheNames.map((cache) => {
if (cache !== cacheName) {
// console.log('Service worker: clearing old cache');
return caches.delete(cache);
}
})
);
})
);
});
// call fetch event
self.addEventListener("fetch", (e) => {
// console.log('Service worker: fetching');
e.respondWith(fetch(e.request).catch(() => caches.match(offlineUrl)));
});