-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
service-worker.js
63 lines (55 loc) · 1.54 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
56
57
58
59
60
61
62
63
const baseURL = `${location.protocol}//${location.host}/`
const booksKey = 'books'
const cacheable = url =>
urls.includes(url.replace(baseURL, '')) ||
(location.hostname === 'localhost' &&
/\.(css|js)$/.test(url) &&
url !== `${baseURL}service-worker.js`)
const listenFetch = () =>
self.addEventListener('fetch', event =>
event.request.url.endsWith('.pdf')
? event.respondWith(
caches
.match(event.request)
.then(response => response || fetch(event.request)),
)
: cacheable(event.request.url) &&
event.respondWith(
navigator.onLine || navigator.onLine === undefined
? fetch(event.request)
.then(response =>
caches.open(shellKey).then(cache => {
cache.put(event.request, response.clone())
return response
}),
)
.catch(() => caches.match(event.request))
: caches.match(event.request),
),
)
const listenInstall = () =>
self.addEventListener('install', event =>
event.waitUntil(
caches
.open(shellKey)
.then(cache => cache.addAll(urls.map(url => url || '.'))),
),
)
const main = () => {
listenFetch()
listenInstall()
}
const shellKey = 'shell'
const urls = [
'',
'assets/favicon.png',
'assets/icon.png',
'assets/icon.svg',
'assets/logo.svg',
'assets/privacy-policy.html',
'assets/splash.png',
'manifest.webmanifest',
'pdfjs/pdf.min.js',
'pdfjs/pdf.worker.min.js',
]
main()