Service Worker | Web Fundamentals

Things to note about a service worker:

  • It is a JavaScript File.
  • They execute on a separate thread from the UI.
  • It can’t access the DOM directly.
  • There is a life cycle or a series of events for a service worker.
  • They are isolated to the origin or domain they are registered with.
  • Service Workers require HTTPS.

Service Workers Work?

A Service worker sits between the browser and the network, acting as a proxy server, handling a collection of non-UI centric tasks. They are event-driven and live outside the browser process, enabling them to work without an active browser session.

Basic Setup of the Service worker

First, make a Basic Html File and add a script in a file (eg: sw.js).

In the script, check if Browser supports ServiceWorker or not?

Then add a script that does all the stuff you want your service worker to do.

If you want to learn or add more features to the code, you can check the above code. (https://github.com/abhayjain13/service-worker)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abhay Jain

Abhay Jain

Developer with 3 yrs of industrial experience in developing scalable web applications.