Service Workers
Javascript Tutorial

Javascript Service workers

Have website-wise Service Workers

Service Workers

Find if browser supports Service Worker

To Check if the browser support supports the service worker API, user the following program code using a javascript.

  • Include the following javascript code in the HTML file one wants to use check the service

  • Recommended
  • Include service worker .js file in the root folder
  • Include the scope as a root / while registering the service worker

/index.html

/js/app.js

/service-worker.js

Register Service worker file with scope

Register the javascript file to the browser's (navigator's) service worker services.

  • Use the browser's serviceWorker object to register javascript (service-worker.js) file with the scope of "/" root or the path one wants to monitor.

Register Service Worker

app.js

Get the registered Service Worker

Registering service worker using navigator.serviceWorker.register will return a Promise

Register service worker

Event listener on Service Worker change

Add an event listener on changing or updating or creating a new service worker

Event listener

Unregister or delete all registered Service Workers

Delete or unregister the service workers using the following code

Event listener

Full life cycle of the Service Worker - sample programs

/index.html

/js/app.js

/service-worker.js


References: