Service workers must be registered from a web page. This is done in a normal UI script. Before you call the register method, you should feature detect service worker support. If supported, the navigator object has a serviceWorker property:
if ('serviceWorker' in navigator) {
}
If the browser supports service workers, you can then safely register your service worker. The serviceWorker object has a register method, so you need to supply a URL reference to the service worker script:
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/sw.js')
.then(function(registration) {
// Registration was successful
});
}
The serviceWorker.register("sw path"[, options]) function accepts two parameters. The first is the path to the service worker. This path is relative to the site origin or root folder.
The...