Skip to content

Instantly share code, notes, and snippets.

@huysentruitw
Last active December 28, 2022 17:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huysentruitw/d7b7043ed0db38f623c39bec3561254f to your computer and use it in GitHub Desktop.
Save huysentruitw/d7b7043ed0db38f623c39bec3561254f to your computer and use it in GitHub Desktop.
sw-registrator.js for medium article about Blazor WASM PWA
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