/
sw.js
47 lines (41 loc) · 1.03 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
const users = [
{id: '1', name: 'JetLu'},
{id: '2', name: 'Gift'}
]
self.addEventListener('fetch', e => {
/**
* 拦截 /api/user/list
*/
if (e.request.url.endsWith('/api/user/list')) {
return e.respondWith(function() {
return new Response(
JSON.stringify({ok: true, data: users}),
{headers: {'content-type': 'application/json'}}
)
}())
}
/**
* 拦截 /api/lottery
*/
if (e.request.url.endsWith('/api/lottery')) {
return e.respondWith(function() {
return new Response(
JSON.stringify({ok: true, data: users}),
{headers: {'content-type': 'application/json'}}
)
}())
}
// 缓存图片资源
if (!/.jpe?g|.png/.test(e.request.url)) return
e.respondWith(async function() {
const cache = await caches.open('v1')
let data = await cache.match(e.request)
if (data) return data
data = await fetch(e.request)
cache.put(e.request, data.clone())
return data
}())
})
self.addEventListener('install', e => {
self.skipWaiting()
})