-
Notifications
You must be signed in to change notification settings - Fork 0
/
serviceworker.js
72 lines (67 loc) · 2.31 KB
/
serviceworker.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
// Install event creates a cache and populates it
self.addEventListener('install', (event) => {
event.waitUntil(
// `./hash.json` is an object containing Webpack's fullhash as the `hash` keys value
fetch('/hash.json').then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
})
.then((obj) => {
const hash = obj.hash;
caches.open('v2').then((cache) => {
return cache.addAll([
'./index.html',
'./404.html',
'./assets/img/header-bg.jpg',
'./favicon.ico',
`./assets/js/dist/main.${hash}.js`,
`./assets/js/dist/main.${hash}.css`,
`./assets/js/dist/25.${hash}.js`,
`./assets/js/dist/29.${hash}.js`,
`./assets/js/dist/58.${hash}.js`,
`./assets/js/dist/189.${hash}.js`,
`./assets/js/dist/314.${hash}.js`,
`./assets/js/dist/579.${hash}.js`,
`./assets/js/dist/636.${hash}.js`,
`./assets/js/dist/671.${hash}.js`,
`./assets/js/dist/676.${hash}.js`,
`./assets/js/dist/839.${hash}.js`,
`./assets/js/dist/909.${hash}.js`,
]);
});
})
.catch((err) => {
console.error(`Error in service worker's pre-install tasks: \n${err}`, err);
})
);
});
// Fetch event serves resources from the cache first, or
// fetches it and adds it to the cache if it doesn't exist:
self.addEventListener('fetch', (event) => {
event.respondWith(
caches.match(event.request).then((resp) => {
return resp || fetch(event.request).then((response) => {
let responseClone = response.clone();
caches.open('v2').then((cache) => {
cache.put(event.request, responseClone);
return response;
}).catch(err => console.error('Not found in cache and no network', err))
});
})
);
});
// Activate event is used to delete old caches once a new service worker is activated:
self.addEventListener('activate', (event) => {
const cacheKeeplist = ['v2']; // Array of cache versions to keep
event.waitUntil(
caches.keys().then((keyList) => {
return Promise.all(keyList.map((key) => {
if (cacheKeeplist.indexOf(key) === -1) {
return caches.delete(key);
}
}));
})
);
});