-
Notifications
You must be signed in to change notification settings - Fork 4
/
service-worker.js
55 lines (51 loc) · 1.49 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const FILES_TO_CACHE = [
'offline.html',
'css/base/variables.css',
'css/base/fonts.css',
'css/base/base.css',
'css/components/comp.css',
'css/components/dialog.css',
'css/components/header.css',
'css/components/pw-po.css',
'css/layouts/logs.css',
'css/layouts/talk.css',
'css/index.css',
'js/swv.js',
'js/index-noncritical.js',
'js/modules/bakeEl.min.js',
'js/frame/frameKeys.js'
];
const CACHE_NAME = 'SWV-cache';
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => {
console.log('[ServiceWorker] Pre-caching offline page.');
return cache.addAll(FILES_TO_CACHE);
})
);
});
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((keyList) => {
return Promise.all(keyList.map((key) => {
if (key !== CACHE_NAME) {
console.log('[ServiceWorker] Removing old cache', key);
return caches.delete(key);
}
}));
})
)
});
self.addEventListener('fetch', (event) => {
if (event.request.destination !== "document" || event.request.mode !== 'navigate') {
return;
}
event.respondWith(
fetch(event.request).catch(() => {
return caches.open(CACHE_NAME)
.then((cache) => {
return cache.match('offline.html');
});
})
);
});