Registering a Service Worker
Follow the step-by-step instructions to register a service worker.
Installing a service worker starts by registering it on the HTML page.
Creating the service worker file
Let’s create a JavaScript file (ending with .js
) in the project’s root folder.
Note: We can give any name to this service worker file. However,
sw.js
andservice-worker.js
are commonly used.
Registering the service worker
The navigator.serviceWorker.register()
method is used to register a service worker on any page. It takes a single argument (path
) that shows the path to the service worker file. It returns a promise.
Get hands-on with 1200+ tech skills courses.