-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsw.js
69 lines (59 loc) · 1.68 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// This is the 'Offline page' service worker
importScripts('https://storage.googleapis.com/workbox-cdn/releases/5.1.2/workbox-sw.js');
const CACHE = 'ega-calculator-cache-v1';
const offlineFallbackPage = '/offline.html';
const filesToCache = [
'/',
'/js/app.js',
'/js/ui.js',
'/js/cyesis.js',
'/css/style.css',
'/img/logo.png',
'/img/icons/48.png',
'/img/icons/96.png',
'/img/icons/120.png',
'/img/icons/144.png',
'/img/icons/150.png',
'/img/icons/256.png',
'/img/icons/512.png',
offlineFallbackPage
];
self.addEventListener('message', (event) => {
if (event.data && event.data.type === 'SKIP_WAITING') {
self.skipWaiting();
}
});
self.addEventListener('install', async (event) => {
event.waitUntil(
(async () => {
const cache = await caches.open(CACHE);
console.log('[ServiceWorker] Pre-caching offline page');
await cache.addAll(filesToCache);
})()
);
});
if (workbox.navigationPreload.isSupported()) {
workbox.navigationPreload.enable();
}
self.addEventListener('fetch', (event) => {
event.respondWith((async () => {
try {
const preloadResp = await event.preloadResponse;
if (preloadResp) {
return preloadResp;
}
const networkResp = await fetch(event.request);
return networkResp;
} catch (error) {
console.log('[Service Worker] Fetch failed; using offline content.');
const cache = await caches.open(CACHE);
let cachedResp;
if (event.request.url === self.location.origin + '/') {
cachedResp = await cache.match(offlineFallbackPage);
} else {
cachedResp = await cache.match(event.request.url);
}
return cachedResp;
}
})());
});