October 2017
Intermediate to advanced
302 pages
7h 27m
English
As we noted earlier, the window.navigator has a serviceWorker property, the existence of which confirms browser support for service workers. We can also use that same object to register our service worker, via its register function. I know, it's shocking stuff.
We call navigator.serviceWorker.register, and pass in the path to our service worker file:
<script> if ('serviceWorker' in navigator) { window.addEventListener('load', function() { navigator.serviceWorker.register('sw.js') }); } else { console.log('service worker is not supported'); }</script>
Read now
Unlock full access