-
Notifications
You must be signed in to change notification settings - Fork 0
/
swreg.js
33 lines (29 loc) · 996 Bytes
/
swreg.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
let newWorker;
let __DISABLE_SW = false;
if ('serviceWorker' in navigator && !__DISABLE_SW) {
let refreshing;
// Esse evento será chamado quando o Service Worker for atualizado
// Aqui estamos recarregando a página
navigator.serviceWorker.addEventListener("controllerchange", function() {
if (refreshing) {
return;
}
window.location.reload();
refreshing = true;
});
navigator.serviceWorker.register('sw.js')
.then(function(reg) {
reg.addEventListener('updatefound', ()=>{
newWorker = reg.installing;
newWorker.addEventListener('statechange', ()=>{
switch(newWorker.state){
case 'installed':
newWorker.postMessage({action: 'skipWaiting'})
}
})
})
console.log('Atualizado com sucesso. Scope: ' + reg.scope);
}).catch(function(error) {
console.error('Falha a registrar pois: ' + error);
});
}