-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice-worker.js
41 lines (38 loc) · 1.03 KB
/
service-worker.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
// Install event: Cache static assets
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open('convertlab-cache-v1').then((cache) => {
return cache.addAll([
'/',
'/index.html',
'/styles.css',
'/script.js',
'/icons/icon-192x192.png',
'/icons/icon-512x512.png',
]);
})
);
});
// Fetch event: Serve cached content when offline
self.addEventListener('fetch', (event) => {
event.respondWith(
caches.match(event.request).then((response) => {
return response || fetch(event.request);
})
);
});
// Activate event: Cleanup old caches
self.addEventListener('activate', (event) => {
const cacheWhitelist = ['convertlab-cache-v1'];
event.waitUntil(
caches.keys().then((keyList) => {
return Promise.all(
keyList.map((key) => {
if (!cacheWhitelist.includes(key)) {
return caches.delete(key);
}
})
);
})
);
});