Skip to content

Instantly share code, notes, and snippets.

@ryanirelan
Created February 7, 2019 15:36
Show Gist options
  • Save ryanirelan/f32ad8018dd960f017822534650e1a71 to your computer and use it in GitHub Desktop.
Save ryanirelan/f32ad8018dd960f017822534650e1a71 to your computer and use it in GitHub Desktop.
Service worker registration code. For livestream at: https://craftquest.io/livestreams/what-are-service-workers
if ('serviceWorker' in navigator) {
window.addEventListener('load', function() {
navigator.serviceWorker.register('/sw.js', {
scope: "/"
}).then(function(registration) {
// Registration was successful
console.log('ServiceWorker registration successful with scope: ', registration.scope);
// Trim the caches on load
navigator.serviceWorker.controller && navigator.serviceWorker.controller.postMessage({
command: "trimCaches"
});
}).catch(function(err) {
// registration failed :(
console.log('ServiceWorker registration failed: ', err);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment