Boopathi Rajaa
Boopathi Rajaa

Reputation: 4729

How to stop older service workers?

The image below shows that there are two workers installed - one active and the other not active (just installed).

two service workers running

  1. Register a service worker
  2. Make changes to service-worker.js and reload the page.
  3. The logic is that Service Workers check binary diff and updates the versions of the workers.

So a new service worker is spawned with a new version ID. But why does the old one keeps running ? and How do I close it ?

The sw.js is here https://gist.github.com/boopathi/57b7e8b6d657d55bdc7d

Upvotes: 7

Views: 3594

Answers (1)

Jeff Posnick
Jeff Posnick

Reputation: 56074

By default, until all tabs that have a page controlled by that old service worker are closed/unloaded, the old service worker will stay running. The new service worker will, well, "wait" in the "waiting" state.

There are options that change this default behavior. They're skipWaiting() and clients.claim().

When skipWaiting() is called from an installing service worker, it will, well, skip the "waiting" state and immediately activate. However, it will not necessarily take control of pages despite being activated—that's what clients.claim() will accomplish.

Upvotes: 12

Related Questions