如何阻止年长的 Service Worker? [英] How to stop older service workers?

查看:53
本文介绍了如何阻止年长的 Service Worker?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下图显示安装了两名工作人员 - 一名处于活动状态,另一名未处于活动状态(刚刚安装).

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

  1. 注册 Service Worker
  2. service-worker.js 进行更改并重新加载页面.
  3. 逻辑是 Service Workers 检查二进制差异并更新 worker 的版本.
  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.

因此,一个新的 Service Worker 会生成一个新的版本 ID.但是为什么旧的一直在运行?以及如何关闭它?

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 ?

sw.js 在这里 https://gist.github.com/boopathi/57b7e8b6d657d55bdc7d

推荐答案

默认情况下,在关闭/卸载具有由该旧 Service Worker 控制的页面的所有选项卡之前,旧 Service Worker 将保持运行.新的 Service Worker 将在等待"状态下等待".

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.

有一些选项可以更改此默认行为.它们是 skipWaiting()clients.claim().

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

skipWaiting() 从安装服务工作者调用时,它会跳过等待"状态并立即激活.然而,尽管被激活,它不一定会控制页面——这就是 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.

这篇关于如何阻止年长的 Service Worker?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆