How to stop older service workers?

The figure below shows that two workers are installed - one active and the other inactive (just installed).

two service workers running

  • Register a service employee
  • Make changes to service-worker.js and reload the page.
  • The logic is that Service Workers check for binary diff and update workers versions.

Thus, a new service worker is created with a new version identifier. But why does the old one work? and how to close it?

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

+6
source share
1 answer

By default, until all tabs that have a page managed by this old service worker are closed / unloaded, the old service worker will work. The new service worker will, well, β€œwait” in a β€œpending” state.

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

When skipWaiting() is called from the service installation worker, it, well, skips the wait state and immediately activates. However, it will not necessarily control the pages, despite the fact that it activates them, what clients.claim() .

+10
source

Source: https://habr.com/ru/post/981348/


All Articles