Created
October 29, 2020 02:15
-
-
Save khu-md/a23b4fd38d6c61974f5d8f9f92099f07 to your computer and use it in GitHub Desktop.
Register a service worker
This file contains hidden or 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
| // 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