-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice-worker.js
50 lines (45 loc) · 1.38 KB
/
service-worker.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
var cacheName = 'Noise主页-v1.3.4';
self.addEventListener('install', function(event) {
event.waitUntil(
caches.open(cacheName).then(function(cache) {
return cache.addAll([
'/',
'https://jsd.onmicrosoft.cn/gh/rcy1314/noisework/assets/',
'https://jsd.onmicrosoft.cn/gh/rcy1314/noisework/css/main.css',
'https://jsd.onmicrosoft.cn/gh/rcy1314/noisework/js/main.js',
// 添加您需要缓存的其他静态资源
]);
})
);
});
self.addEventListener('fetch', function(event) {
event.respondWith(
caches.match(event.request).then(function(response) {
if (response) {
return response;
}
// 如果请求未在缓存中找到,则发起网络请求
return fetch(event.request).then(function(networkResponse) {
// 将请求的响应添加到缓存中
caches.open(cacheName).then(function(cache) {
cache.put(event.request, networkResponse.clone());
});
return networkResponse;
});
})
);
});
self.addEventListener('activate', function(event) {
event.waitUntil(
caches.keys().then(function(cacheNames) {
return Promise.all(
cacheNames.filter(function(name) {
// 删除旧版本的缓存
return name !== cacheName;
}).map(function(name) {
return caches.delete(name);
})
);
})
);
});