Skip to content

Instantly share code, notes, and snippets.

@charisTheo
Created October 14, 2019 12:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charisTheo/ae9b0fc1d47c3bc991027e1ec326f40e to your computer and use it in GitHub Desktop.
Save charisTheo/ae9b0fc1d47c3bc991027e1ec326f40e to your computer and use it in GitHub Desktop.
Check if a new version of the website is available and show a notification to the user for reloading the page in order to see the new website
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/service-worker.js', {scope: '/'})
.then(function(registration) {
registration.onupdatefound = () => {
const newServiceWorker = registration.installing;
newServiceWorker.onstatechange = () => {
switch (newServiceWorker.state) {
case 'installing':
if (navigator.serviceWorker.controller) {
document.getElementById('new-version-banner').innerHTML('A new version of the website is available 🙋. <a onclick="window.location.reload()">Reload</a> to see all the new goodness 💠');
}
}
}
}
console.log('Service Worker registration succeeded. Scope is ' + registration.scope);
}).catch(function(error) {
console.log('Service Worker registration failed with ' + error);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment