-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
81 lines (72 loc) · 2.69 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
70
71
72
73
74
75
76
77
78
79
const filesToCache = [
"html/index.html",
"css/master.css",
"css/masterOG.css",
"js/index.js",
"data/products.json",
"data/bleresit.json",
"js/table.js",
"js/jquery.js",
"images/remove.png",
"images/minusWhite.png",
// "https://fonts.googleapis.com/css2?family=Poppins:ital,wght@1,200&display=swap",
// "https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,300;1,200&display=swap",
// "https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,300;0,500;1,200&display=swap",
// "https://fonts.googleapis.com/css2?family=DM+Mono&family=Nova+Mono&family=Poppins:ital,wght@0,300;0,500;1,200&family=Space+Mono&display=swap",
// "https://fonts.googleapis.com/css2?family=DM+Mono:ital,wght@0,400;1,500&family=Nova+Mono&family=Poppins:ital,wght@0,300;0,500;1,200&family=Space+Mono&display=swap",
// "https://fonts.googleapis.com/css2?family=Roboto+Mono&display=swap",
// "https://fonts.googleapis.com/css2?family=Roboto&display=swap"
"fonts/robotoMono.woff2",
"fonts/poppins.woff2"
];
const staticCacheName = 'pages-cache-v11';
self.addEventListener('install', event => {
//console.log('Attempting to install service worker and cache static assets');
event.waitUntil(
caches.open(staticCacheName)
.then(cache => {
return cache.addAll(filesToCache);
})
);
});
addEventListener('activate', event => {
event.waitUntil(
// Feature-detect
// if (self.registration.navigationPreload) {
// // Enable navigation preloads!
// await self.registration.navigationPreload.enable();
// }
caches.keys()
.then(cacheNames => {
cacheNames.map(cache => {
if(cache !== staticCacheName){
return caches.delete(cache);
}
})
})
)
});
addEventListener('fetch', (event) => {
const { request } = event;
// Always bypass for range requests, due to browser bugs
if (request.headers.has('range')) return;
event.respondWith(async function() {
// Try to get from the cache:
const cachedResponse = await caches.match(request);
if (cachedResponse) return cachedResponse;
try {
// See https://developers.google.com/web/updates/2017/02/navigation-preload#using_the_preloaded_response
const response = await event.preloadResponse;
if (response) return response;
// Otherwise, get from the network
return await fetch(request);
} catch (err) {
// If this was a navigation, show the offline page:
if (request.mode === 'navigate') {
return caches.match('index.html');
}
// Otherwise throw
throw err;
}
}());
});