Last active
December 28, 2022 17:01
-
-
Save huysentruitw/d7b7043ed0db38f623c39bec3561254f to your computer and use it in GitHub Desktop.
sw-registrator.js for medium article about Blazor WASM PWA
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.js') | |
.then(registration => { | |
console.info(`Service worker registration successful (scope: ${registration.scope})`); | |
registration.onupdatefound = () => { | |
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); | |
}); | |
}); | |
window.registerForUpdateAvailableNotification = (caller, methodName) => { | |
window.updateAvailable.then(isUpdateAvailable => { | |
if (isUpdateAvailable) { | |
caller.invokeMethodAsync(methodName).then(); | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment