Created
October 14, 2019 12:12
-
-
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
This file contains 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
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