-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
52 lines (48 loc) · 1.56 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
// 定义版本号和缓存名称
var cacheStorageKey = 'minimal-pwa-29';
// 定义需要缓存的文件列表
var cacheList = [
'/',
"https://jacobhu0723.github.io/Huge-Clock/index.html",
"https://jacobhu0723.github.io/Huge-Clock/manifest.json",
"https://jacobhu0723.github.io/Huge-Clock/js/audio.js",
"https://jacobhu0723.github.io/Huge-Clock/js/NoSleep.min.js",
];
// 监听 install 事件,在安装 Service Worker 时缓存文件
self.addEventListener('install', e => {
e.waitUntil(
caches.open(cacheStorageKey)
.then(cache => cache.addAll(cacheList))
.then(() => self.skipWaiting())
);
});
// 监听 activate 事件,在激活 Service Worker 时清除旧缓存
self.addEventListener('activate', function(e) {
e.waitUntil(
Promise.all([
caches.keys().then(cacheNames => {
return cacheNames.map(name => {
if (name !== cacheStorageKey) {
return caches.delete(name);
}
});
})
]).then(() => {
return self.clients.claim();
})
);
});
// 监听 fetch 事件,在发起网络请求时返回缓存的响应
self.addEventListener('fetch', event => {
console.log('[Service Worker] Fetching something ....', event);
// This fixes a weird bug in Chrome when you open the Developer Tools
if (event.request.cache === 'only-if-cached' && event.request.mode !== 'same-origin') {
return;
}
event.respondWith(
caches.match(event.request)
.then(response => {
return response || fetch(event.request);
})
);
});