/
sw.js
69 lines (61 loc) · 2.29 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
const OFFLINE_VERSION = 1;
const CACHE_NAME = 'suplike';
const OFFLINE_URL = 'offline.html';
self.addEventListener('install', (event) => {
event.waitUntil((async () => {
const cache = await caches.open(CACHE_NAME);
await cache.add(new Request(OFFLINE_URL, { cache: 'reload' }));
await cache.addAll([
// List the URLs of the files in the 'libs' folder
'./lib/font-awesome/css/all.min.css',
'./lib/font-awesome/webfonts/fa-solid-900.woff2',
'./lib/font-awesome/webfonts/fa-regular-400.woff2',
'./lib/bootstrap/css/bootstrap.min.css',
'./lib/bootstrap/js/bootstrap.bundle.min.js',
]);
})());
});
self.addEventListener('activate', (event) => {
event.waitUntil((async () => {
if ('navigationPreload' in self.registration) {
await self.registration.navigationPreload.enable();
}
})());
// Tell the active service worker to take control of the page immediately.
self.clients.claim();
});
self.addEventListener('fetch', (event) => {
// We only want to call event.respondWith() if this is a navigation request
// for an HTML page.
if (event.request.mode === 'navigate') {
event.respondWith((async () => {
try {
// First, try to use the navigation preload response if it's supported.
const preloadResponse = await event.preloadResponse;
if (preloadResponse) {
return preloadResponse;
}
// Check if the requested resource is one of the library files
const requestUrl = new URL(event.request.url);
if (
requestUrl.href.includes('lib/font-awesome/') ||
requestUrl.href.includes('lib/bootstrap/')
) {
const cache = await caches.open(CACHE_NAME);
const cachedResponse = await cache.match(event.request);
if (cachedResponse) {
return cachedResponse;
}
}
// If the requested resource is not in cache, fetch it from the network
const networkResponse = await fetch(event.request);
return networkResponse;
} catch (error) {
console.log('Fetch failed; returning offline page instead.', error);
const cache = await caches.open(CACHE_NAME);
const cachedResponse = await cache.match(OFFLINE_URL);
return cachedResponse;
}
})());
}
});