-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsw.js
45 lines (45 loc) · 1.27 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
const appName = 'swpt-debtors-ui'
const cacheName = `${appName}-v1.2.1`
const appFiles = [
'./',
'index.html',
'manifest.json',
'favicon.svg',
'global.css',
'config.js',
'material.min.css',
'material-icons.css',
'fonts/material-icons.woff2',
'fonts/Roboto_300.woff2',
'fonts/Roboto_400.woff2',
'fonts/Roboto_500.woff2',
'fonts/Roboto_700.woff2',
'fonts/Roboto_Mono.woff2',
'fonts/Cutive_Mono.woff2',
'build/main.css',
'build/main.js',
'build/qr-scanner-worker.min.js',
]
self.addEventListener('install', (e) => {
console.log('[Service Worker] Install')
e.waitUntil((async () => {
const cache = await caches.open(cacheName)
console.log('[Service Worker] Caching app files')
await cache.addAll(appFiles)
})())
})
self.addEventListener('fetch', (e) => {
e.respondWith((async () => {
const cache = await caches.open(cacheName)
const r = await cache.match(e.request, {ignoreSearch: true})
if (r) { return r }
return await fetch(e.request)
})())
})
self.addEventListener('activate', async (e) => {
e.waitUntil((async () => {
const keyList = await caches.keys()
const deleteList = keyList.filter(key => key.startsWith(appName) && key !== cacheName)
await Promise.all(deleteList.map(key => caches.delete(key)))
})())
})