-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsw.js
45 lines (45 loc) · 1.15 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
importScripts(
'https://storage.googleapis.com/workbox-cdn/releases/3.1.0/workbox-sw.js'
);
var cacheStorageKey = 'weishaodaren_is_fuckin_real';
var cacheList = ['/', 'index.html'];
self.addEventListener('install', (e) => {
e.waitUntil(
caches
.open(cacheStorageKey)
.then((cache) => cache.addAll(cacheList))
.then(() => self.skipWaiting())
);
});
self.addEventListener('fetch', function (e) {
e.respondWith(
caches.match(e.request).then(function (response) {
if (response != null) {
return response;
}
return fetch(e.request.url);
})
);
});
self.addEventListener('activate', function (e) {
e.waitUntil(
//获取所有cache名称
caches
.keys()
.then((cacheNames) => {
return Promise.all(
// 获取所有不同于当前版本名称cache下的内容
cacheNames
.filter((cacheNames) => {
return cacheNames !== cacheStorageKey;
})
.map((cacheNames) => {
return caches.delete(cacheNames);
})
);
})
.then(() => {
return self.clients.claim();
})
);
});