Make sure you load the page in localhost or you need to use https
You will also need to serve your code over HTTPS. Service workers can only work on HTTPS for security reasons. Therefore, GitHub is a good place to experiment because it supports HTTPS.
https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker_API/Using_Service_Workers
Check if your browser supports this feature.
if ('serviceWorker' in navigator) { [...] } else { console.log("this browser does NOT support service worker"); }
This may help: http://caniuse.com/#feat=serviceworkers
If you want to see the current status of your serviceworker, you can do something like this:
navigator.serviceWorker.register('/serviceworker.js', {scope: '/'}) .then(function (registration) { var serviceWorker; if (registration.installing) { serviceWorker = registration.installing; } else if (registration.waiting) { serviceWorker = registration.waiting; } else if (registration.active) { serviceWorker = registration.active; } if (serviceWorker) { console.log("ServiceWorker phase:", serviceWorker.state); serviceWorker.addEventListener('statechange', function (e) { console.log("ServiceWorker phase:", e.target.state); }); } }).catch(function (err) { console.log('ServiceWorker registration failed: ', err); });
source share