Skip to content

Instantly share code, notes, and snippets.

@biggerpicturestudio
Created April 12, 2017 10:30
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 biggerpicturestudio/9cbeacfb56a943e49f0640b0f44e38c5 to your computer and use it in GitHub Desktop.
Save biggerpicturestudio/9cbeacfb56a943e49f0640b0f44e38c5 to your computer and use it in GitHub Desktop.
if ('serviceWorker' in navigator && location.hostname !== "localhost") {
window.addEventListener('load', function() {
navigator.serviceWorker.register(‚service-worker.js’).then(function(reg) {
// updatefound is fired if service-worker.js changes.
reg.onupdatefound = function() {
var installingWorker = reg.installing;
installingWorker.onstatechange = function() {
switch (installingWorker.state) {
case 'installed':
if (navigator.serviceWorker.controller) {
setTimeout(function() {
location.reload(true);
}, 5000);
} else {
// At this point, everything has been precached.
// It's the perfect time to display a "Content is cached for offline use." message.
console.log('Content is now available offline!');
}
break;
case 'redundant':
console.error('The installing service worker became redundant.');
break;
}
};
};
}).catch(function(e) {
console.error('Error during service worker registration:', e);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment