-
Notifications
You must be signed in to change notification settings - Fork 5
/
sw.js
71 lines (64 loc) · 2.02 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
const version = 1;
const cacheStorageKey = "testCache-" + version;
// 这是需要预缓存的资源,也可以是appshell,可以通过webpack的插件来生成
const cacheList = ["/", "index.html", "main.css", "e.png", "pwa-fonts.png"];
// 注册成功的时候,以版本名为key主动缓存静态资源
self.addEventListener("install", function(e) {
console.log("Cache event!");
e.waitUntil(
caches.open(cacheStorageKey).then(function(cache) {
console.log("Adding to Cache:", cacheList);
return cache.addAll(cacheList);
})
// .then(function() {
// // 注册成功跳过等待,酌情处理
// // console.log('Skip waiting!')
// // return self.skipWaiting()
// })
);
});
// 当新的serviceWorker被激活时,删除旧版本的缓存
self.addEventListener("activate", event => {
console.log("Activate event");
event.waitUntil(
caches
.keys()
.then(cacheNames => {
return cacheNames.filter(cacheName => cacheStorageKey !== cacheName);
})
.then(cachesToDelete => {
return Promise.all(
cachesToDelete.map(cacheToDelete => {
return caches.delete(cacheToDelete);
})
);
})
.then(() => {
console.log("Clients claims.");
// 立即接管所有页面,酌情处理
// 会导致新的sw接管旧的页面,同时旧版本的缓存已被清空
self.clients.claim();
})
);
});
// 发起请求时去根据uri去匹配缓存,无法命中缓存则发起请求
self.addEventListener("fetch", function(e) {
e.respondWith(
caches.match(e.request).then(function(response) {
return response || fetch(e.request);
})
);
});
self.addEventListener("message", event => {
if (event.data === "skipWaiting") {
console.log("Skip waiting!");
self.skipWaiting();
}
});
// self.clients.matchAll().then(function(clients) {
// if (clients && clients.length) {
// clients.forEach(function(client) {
// client.postMessage("msg");
// });
// }
// });