Repository navigation
Expand file tree
/
Copy pathsw.js
More file actions
executable file
·46 lines (40 loc) · 1.44 KB
/
Copy pathsw.js
File metadata and controls
executable file
·46 lines (40 loc) · 1.44 KB
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
// Import Workbox
importScripts('https://storage.googleapis.com/workbox-cdn/releases/6.4.1/workbox-sw.js');
const { registerRoute } = workbox.routing;
const { CacheFirst, NetworkFirst } = workbox.strategies;
const { clientsClaim, skipWaiting } = workbox.core;
const cacheName = 'Beacon-cache-v1.0.7'; // Change version number when you update
// Activate new service worker immediately and claim clients
self.addEventListener('install', (event) => {
self.skipWaiting(); // Forces new SW to take control immediately
});
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames.map(cache => {
if (cache !== cacheName) {
console.log(`Deleting old cache: ${cache}`);
return caches.delete(cache);
}
})
);
})
);
clientsClaim(); // Forces all open clients to be controlled by new SW
});
// Cache strategy for assets
registerRoute(
({ request }) => ['script', 'style', 'document', 'image', 'font', 'audio', 'video'].includes(request.destination),
new CacheFirst({
cacheName: cacheName,
})
);
// Update checking for `version.json`
registerRoute(
({ url }) => url.pathname.endsWith('/version.json'),
new NetworkFirst({
cacheName: 'version-cache',
networkTimeoutSeconds: 3,
})
);