blob: 432e630fec69fb9fb9219cd1fd0dc210d021f566 (
plain) (
blame)
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 cache_name = "v3"
const install_sw = async event => {
const precache = async() => {
const cache = await caches.open(cache_name);
return cache.addAll([
'/index.css',
'/prism.css',
'/index.js',
'/sw.js',
'/favicon.svg',
'/offline.html',
'/'
])
}
event.waitUntil(precache())
}
const activate_sw = async event => {
const cachepreserve = ['v3'];
const invalidatecache = async() => {
const keys = await caches.keys()
Promise.all(
keys.map((key) => {
if (cachepreserve.indexOf(key) === -1) {
return caches.delete(key)
}
}))
}
event.waitUntil(invalidatecache())
}
const fetch_sw = async event => {
const cache_fetch = async() => {
if (event.request.method != 'GET') {
return
}
const cacheres = await caches.match(event.request)
if (cacheres !== undefined) {
return cacheres
}
try {
const response = await fetch(event.request)
const cache = await caches.open(cache_name)
cache.put(event.request, response.clone())
return response
} catch {
return caches.match('/offline.html')
}
}
event.respondWith(cache_fetch())
}
const main = async() => {
self.addEventListener('install', install_sw)
self.addEventListener('activate', activate_sw)
self.addEventListener('fetch', fetch_sw)
}
main()
|