pinafore/routes/_utils/serviceWorkerClient.js

22 lines
751 B
JavaScript
Raw Normal View History

2018-01-19 08:37:43 +01:00
import { toast } from './toast'
import { keyval } from './database/keyval'
function onUpdateFound(registration) {
const newWorker = registration.installing
newWorker.addEventListener('statechange', async () => {
if (!(await keyval.get('serviceworker_installed'))) {
await keyval.set('serviceworker_installed', true)
return
}
if (newWorker.state === 'activated' && navigator.serviceWorker.controller) {
toast.say('App update available. Reload to update.')
}
});
}
if (!location.origin.match('localhost') && 'serviceWorker' in navigator) {
navigator.serviceWorker.register('/service-worker.js').then(registration => {
registration.addEventListener('updatefound', () => onUpdateFound(registration))
})
}