Created
October 31, 2018 16:23
-
-
Save i-oliva/b1f46f3d9de3503d3d7c3b44d21829a8 to your computer and use it in GitHub Desktop.
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
function registerValidSW(swUrl) { | |
navigator.serviceWorker | |
.register(swUrl) | |
.then((registration) => { | |
registration.onupdatefound = () => { | |
const installingWorker = registration.installing; | |
installingWorker.onstatechange = () => { | |
if (installingWorker.state === 'installed') { | |
if (navigator.serviceWorker.controller) { | |
// At this point, the old content will have been purged and | |
// the fresh content will have been added to the cache. | |
// It's the perfect time to display a "New content is | |
// available; please refresh." message in your web app. | |
console.log('New content is available; please refresh.'); | |
} 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 cached for offline use.'); | |
} | |
} | |
}; | |
}; | |
}) | |
.catch((error) => { | |
console.error('Error during service worker registration:', error); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment