The image below shows that there are two workers installed - one active and the other not active (just installed).
![](https://www.manongdao.com/static/images/pcload.jpg)
- Register a service worker
- Make changes to
service-worker.js
and reload the page.
- 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
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.