pinafore/routes/_utils/serviceWorkerClient.js

18 lines
576 B
JavaScript
Raw Normal View History

2018-01-19 08:37:43 +01:00
import { toast } from './toast'
2018-02-09 07:29:29 +01:00
function onUpdateFound (registration) {
2018-01-19 08:37:43 +01:00
const newWorker = registration.installing
newWorker.addEventListener('statechange', async () => {
2018-01-19 09:29:45 +01:00
if (newWorker.state === 'installed' && navigator.serviceWorker.controller) {
2018-03-15 07:17:47 +01:00
toast.say('App update available. Reload to update.')
2018-01-19 08:37:43 +01:00
}
2018-02-09 07:29:29 +01:00
})
2018-01-19 08:37:43 +01:00
}
if (!location.origin.match('localhost') && 'serviceWorker' in navigator) {
navigator.serviceWorker.register('/service-worker.js').then(registration => {
registration.addEventListener('updatefound', () => onUpdateFound(registration))
})
2018-02-09 07:29:29 +01:00
}