Skip to content

Instantly share code, notes, and snippets.

@khu-md
Created October 29, 2020 02:15
Show Gist options
  • Select an option

  • Save khu-md/a23b4fd38d6c61974f5d8f9f92099f07 to your computer and use it in GitHub Desktop.

Select an option

Save khu-md/a23b4fd38d6c61974f5d8f9f92099f07 to your computer and use it in GitHub Desktop.
Register a service worker
// Client-side JavaScript can change element colors
const title = document.getElementById("title");
title.style.color = "blue";
// It can also register a service worker.
/**
* REGISTRATION
*
* tests for if navigator even has serviceWorker API
* if so, adds an event listener onto window's "load" event
*/
if ("serviceWorker" in navigator) {
window.addEventListener("load", function () {
navigator.serviceWorker
.register("/sw.js")
.then(function (registration) {
// Registration successful
console.log(
"ServiceWorker registration successful with scope: ",
registration.scope
);
})
.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