-
Notifications
You must be signed in to change notification settings - Fork 2
/
sw.js
81 lines (64 loc) · 2.36 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
let cacheName = '缓存';
// 缓存文件列表
const cacheList = [
// 基本缓存文件
'./public/image/favicon.jpg',
'./assets/plugin/cannan/animate.css',
'./assets/plugin/cannan/canaan.css',
'./assets/plugin/cannan/canaan.div.release.js',
'./assets/plugin/weixin-js-sdk/jweixin-1.6.0.js'
]
// 执行缓存操作
this.addEventListener('install', event => {
this.skipWaiting()// 装完直接用
event.waitUntil(
caches.open(cacheName).then(cache => {
if (cache) cache.addAll(cacheList)
}).catch(e => {
console.log(e)
})
)
})
// 激活。清空上个service worker保存的内容
this.addEventListener('activate', event => {
event.waitUntil(
caches.keys().then((nameList) => {
return Promise.all(
nameList.map((i) => {
if (i !== cacheName) {
return caches.delete(i)
}
})
)
// for(var i = 0; i < nameList.length; i++){
// if(i !== cacheName) caches.delete(i);
// }
}).catch(e => console.log(e))
)
})
// 监听请求
this.addEventListener('fetch', event => {
event.respondWith(
caches.match(event.request)
.then(res => {
// 匹配到缓存直接返回
if (res) return res
const requestClone = event.request.clone() // 复制请求头
// 直接请求,并返回
// return fetch(requestClone).then(new_res => new_res)
// 直接请求,缓存后返回
return fetch(requestClone).then(new_res => {
if (!new_res || new_res.status !== 200) return new_res // 响应报错,直接返回
const responseClone = new_res.clone() // 复制响应
// 除了php, html, application, json 不缓存,其他都缓存
const pattern = /\/$|html|php/ig
if (!pattern.test(new_res.url)) { caches.open(cacheName).then(cache => cache && cache.put(requestClone, responseClone)).catch(e => console.log(e)) } // 进行缓存
return new_res
}).catch(e => {
console.log(e)
})
}).catch(e => {
console.log(e)
})
)
})