-
Notifications
You must be signed in to change notification settings - Fork 1
/
sw.js
65 lines (58 loc) · 1.91 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
63
64
65
const CACHE_NAME = 'fb2-reader-v0.0.12';
const urlsToCache = [
'./',
'index.html',
'favicon.ico',
'fb2-html.xsl',
'main.css',
'js/book-position.js',
'js/book-storage.js',
'js/bookshelf.js',
'js/main.js',
'js/process-fb2.js',
'js/swipe.js',
'js/utils.js',
'js/thirdparty/idb-keyval.js',
'js/thirdparty/unzipit.module.js',
'js/thirdparty/unzipit-worker.module.js',
'js/components/battery-indicator.js',
'js/components/clock-indicator.js',
'js/components/font-selector.js',
'js/components/fullscreen-button.js',
'js/components/index.js',
'js/components/theme-selector.js',
'js/components/utils.js',
];
// Install a service worker
self.addEventListener('install', event => event.waitUntil(
(async () => {
// Perform install steps
const cache = await caches.open(CACHE_NAME);
await Promise.all(urlsToCache.map(url=> cache.add(url)));
})()
));
// Cache lookup and fetch the request
self.addEventListener('fetch', (event) => event.respondWith(
(async () => {
const cachedResponse = await caches.match(event.request);
if (cachedResponse) return cachedResponse;
const response = await fetch(event.request);
if (
!response ||
!response.ok ||
response.type !== "basic" ||
!event.request.url.startsWith('http')
) return response;
const responseForCache = response.clone();
caches.open(CACHE_NAME).then(cache => cache.put(event.request, responseForCache));
return response;
})()
));
// Update a service worker
self.addEventListener('activate', event => event.waitUntil(
(async () => {
const cacheKeys = await caches.keys();
const filteredKeys = cacheKeys.filter(cacheName => cacheName !== CACHE_NAME);
await Promise.all(filteredKeys.map(cacheName => caches.delete(cacheName)));
})()
));