How to stop older service workers?

孤人 提交于 2019-12-23 09:18:29

问题


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

  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


回答1:


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.



来源:https://stackoverflow.com/questions/28069249/how-to-stop-older-service-workers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!