This repository has been archived by the owner on Jul 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
sw.js
62 lines (59 loc) · 1.72 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
const CACHE_NAME = 'PWA-v13';
const expectedCaches = [CACHE_NAME];
const staticFiles = [
'./',
'./main.css',
'./build/bundle.js',
'./css/materialize.min.css',
'./fonts/MaterialIcons-Regular.eot',
'./fonts/MaterialIcons-Regular.ijmap',
'./fonts/MaterialIcons-Regular.svg',
'./fonts/MaterialIcons-Regular.ttf',
'./fonts/MaterialIcons-Regular.woff',
'./fonts/MaterialIcons-Regular.woff2',
'./images/touch/gamepad_144x144.png',
'./images/maps/de-cache.png',
'./images/maps/de-cobble.png',
'./images/maps/de-dust2.png',
'./images/maps/de-inferno.png',
'./images/maps/de-mirage.png',
'./images/maps/de-nuke.png',
'./images/maps/de-overpass.png',
'./images/maps/de-train.png',
'./favicon.ico'
];
/**
* Performs install steps.
*/
self.addEventListener('install', (event) => {
self.skipWaiting();
event.waitUntil(
caches.open(CACHE_NAME).then(cache => cache.addAll(staticFiles))
);
});
/**
* Handles requests: responds with cache or else network.
*/
self.addEventListener('fetch', (event) => {
const url = new URL(event.request.url);
event.respondWith(
caches.match(event.request).then(response => response || fetch(event.request))
);
});
/**
* Cleans up static cache and activates the Service Worker.
*/
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then(keys => Promise.all(
keys.map((key) => {
if (!expectedCaches.includes(key)) {
return caches.delete(key);
}
})
)).then(() => {
console.log(`${CACHE_NAME} now ready to handle fetches!`);
return clients.claim();
})
);
});