-
Notifications
You must be signed in to change notification settings - Fork 1
/
sw.js
61 lines (53 loc) · 1.35 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
const version = '1.2.1'
const files = [
'/',
'/index.css',
'/index.js',
'/manifest.json',
'/icons/favicon-16x16.png',
'/icons/favicon-32x32.png',
'/icons/android-chrome-192x192.png',
'/icons/apple-touch-icon.png',
'/modules/url-state/index.js',
'/modules/url-state/qs.js',
'/modules/hyperbind/index.js',
'/state/index.js',
'/raw/index.js',
'/can/index.js',
'/disconnected/index.js',
'/disconnected/index.css',
'/connecting/index.js',
'/connected/index.js',
'/connected/index.css',
'/failure/index.css',
'/failure/index.js',
]
class MyWorker {
async install (evt) {
const cache = await caches.open(version)
const r = await cache.addAll(files)
return r
}
async fetch (evt) {
const cache = await caches.open(version)
const res = await cache.match(evt.request)
if (res) return res
return fetch(evt.request)
}
async clearCache () {
const allCaches = await caches.keys()
const jobs = allCaches.map(key => key !== version ? caches.delete(key) : null)
return Promise.all(jobs)
}
}
const w = new MyWorker()
self.addEventListener('install', evt => {
evt.waitUntil(w.install(evt))
})
self.addEventListener('activate', evt => {
evt.waitUntil(w.clearCache())
})
self.addEventListener('fetch', evt => {
// console.log(new URL(evt.request.url).pathname)
evt.respondWith(w.fetch(evt))
})