JS
S
JavaScriptRegister A service worker To install a service worker you need to kick start the process by registering it in your page. This tells the browser where your service worker JavaScript file lives. This code checks to see if the service worker API is available, and if it is, the service worker at /sw.js is registered once the page is loaded.
1if ('serviceWorker' in navigator) {
2 window.addEventListener('load', function() {
3 navigator.serviceWorker.register('/sw.js').then(function(registration) {
4 // Registration was successful
5 console.log('ServiceWorker registration successful with scope: ', registration.scope);
6 }, function(err) {
7 // registration failed :(
8 console.log('ServiceWorker registration failed: ', err);
9 });
10 });
11}
Created on 6/21/2018