-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsw-registrator.js
More file actions
37 lines (33 loc) · 1.26 KB
/
sw-registrator.js
File metadata and controls
37 lines (33 loc) · 1.26 KB
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
window.updateAvailable = new Promise((resolve, reject) => {
if (!('serviceWorker' in navigator)) {
const errorMessage = 'This browser doesn\'t support service workers'
console.error(errorMessage)
reject(errorMessage)
return
}
navigator.serviceWorker.register('/service-worker.min.js')
.then(registration => {
console.info(`Service worker registration successful (scope: ${registration.scope})`)
setInterval(() => {
registration.update()
}, 60 * 1000) // 60000ms -> check each minute
registration.onupdatefound = () => {
console.info(`Service worker onupdatefound event`)
const installingServiceWorker = registration.installing
installingServiceWorker.onstatechange = () => {
if (installingServiceWorker.state === 'installed') {
resolve(!!navigator.serviceWorker.controller)
}
}
}
})
.catch(error => {
console.error('Service worker registration failed with error:', error)
reject(error)
})
navigator.serviceWorker.addEventListener('controllerchange', () => {
console.log('Service worker controller changed, reloading page')
window.location.reload()
resolve(true)
});
})