forked from vickkie/Hypercritical
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhypercritical-worker.js
47 lines (39 loc) · 1.16 KB
/
hypercritical-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
const cacheName = "Hypercritical-offline";
let offlineUrl = "offline.html";
let offlineFont = "fonts/SpaceGrotesk.woff";
let offlineMask = "assets/masks/hypercritical-main-logo.png";
const cacheAssets = [offlineUrl, offlineFont, offlineMask];
//calling 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)));
});