1
0
mirror of https://github.com/nolanlawson/pinafore synced 2024-12-12 18:28:03 +01:00
Pinafore-Web-Client-Frontend/routes/_utils/serviceWorkerClient.js

17 lines
572 B
JavaScript
Raw Normal View History

2018-01-19 08:37:43 +01:00
import { toast } from './toast'
function onUpdateFound(registration) {
const newWorker = registration.installing
newWorker.addEventListener('statechange', async () => {
2018-01-19 09:29:45 +01:00
if (newWorker.state === 'installed' && navigator.serviceWorker.controller) {
toast.say('Update available. Refresh to update.')
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))
})
}