How to register a service worker in JavaScript

PWA
Last updated Mar 12, 2018
if ("serviceWorker" in navigator) {
  window.addEventListener("load", () => {
    navigator.serviceWorker
      .register("/sw.js")
      .then(registration => console.log(registration))
      .catch(err => console.error(err));
  });
}