-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserviceWorker.js
35 lines (35 loc) · 1.08 KB
/
serviceWorker.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
if ('serviceWorker' in navigator) {
window.addEventListener('load', function() {
const sw = navigator.serviceWorker;
/**
* 卸载
*/
function unRegister(){
sw.getRegistration('sw').then(registration => {
// 手动注销
registration.unregister();
// 清除缓存
window.caches && caches.keys && caches.keys().then((keys) => {
keys.forEach(function(key) {
caches.delete(key);
});
});
});
}
// 兜底方案
fetch('/fallback?project=1').then((relegation) => {
// 服务端通知强行降级
if(relegation){
unRegister();
return;
}
sw.register('sw.js').then(() => {}).catch((e) => {
console.error('Error during service worker registration:', e)
unRegister();
});
})
.catch(() => {
unRegister()
})
});
}