-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
service-worker.ts
66 lines (48 loc) · 1.42 KB
/
service-worker.ts
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
/// <reference path="../node_modules/typescript/lib/lib.webworker.d.ts" />
declare let self: ServiceWorkerGlobalScope
const ASSET_CACHE = 'asset_cache'
self.addEventListener('install', event => {
event.waitUntil((async () => {
let asset_cache = await caches.open(ASSET_CACHE)
await asset_cache.addAll(ASSETS)
})())
})
self.addEventListener('fetch', event => {
const { request } = event
if (
request.url.match(/(search)$/) ||
!request.url.match(/dict|unpkg/)
) return
// --- assets
event.respondWith((async () => {
const cache = await caches.open(ASSET_CACHE)
const cache_data = await cache.match(request)
if (cache_data)
return cache_data
const result = await fetch(request)
cache.put(request, result.clone())
return result
})())
})
const ASSETS = [
'index.html',
'manifest.json',
'index.js',
'dict.ico',
'dict.png',
'dict.192.png',
// 会变成 url 打包到 index.js 中
// 'search.png',
'https://unpkg.com/jquery',
'https://unpkg.com/lodash',
'https://unpkg.com/react/umd/react.development.js',
'https://unpkg.com/react-dom/umd/react-dom.development.js',
'lib/pcmdata.min.js',
'lib/aurora.js',
'lib/bitstring.js',
'lib/aurora-speex.js',
'lib/ogg.min.js',
'lib/speex.js',
'lib/audio.js',
]
export { }