Workbox:self.skipWaiting() 的危险 [英] Workbox: the danger of self.skipWaiting()

查看:168
本文介绍了Workbox:self.skipWaiting() 的危险的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Workbox 来预缓存呈现 App Shell 所需的资产,包括基本版本的 index.html.Workbox 假设 index.html 在缓存中可用,否则页面导航失败,因为我在 Service Worker 中注册了它:

I use Workbox to pre-cache assets required to render the app shell, including a basic version of index.html. Workbox assumes that index.html is available in cache, otherwise, page navigation fails because I have this registered in my Service Worker:

workbox.routing.registerNavigationRoute('/index.html');

我在安装监听器中也有 self.skipWaiting() 指令:

I also have the self.skipWaiting() instruction in the install listener:

self.addEventListener('install', e => {
  self.skipWaiting();
});

据我所知,现在有 2 个 install 监听器:

As I understand it, there are 2 install listeners now:

  • Workbox 为预缓存资产(包括 index.html)注册的一个
  • 我在 Service Worker 中手动注册的一个

self.skipWaiting() 是否有可能在 Workbox 的安装侦听器失败时成功?这将导致出现问题状态,即资产未预先缓存但 Service Worker 已激活.这种情况是否可能发生,我应该防范吗?

Is it possible for self.skipWaiting() to succeed while Workbox's install listener fails? This would lead to a problematic state where assets don't get pre-cached but the Service Worker is activated. Is such a scenario possible and should I protect against it?

推荐答案

我强烈推荐 "Service Worker 生命周期"作为有关 Service Worker 安装和更新不同阶段的权威信息来源.

I highly recommend "The Service Worker Lifecycle" as an authoritative source of information about the different stages of a service worker's installation and updating.

总结该文章中的一些信息,因为它适用于您的问题:

To summarize some info from that article, as it applies to your question:

  • Service Worker 首先进入 installing 阶段,无论您注册了多少 install 侦听器,它们都将有机会执行.正如您所建议的,Workbox 创建了自己的 install 侦听器来处理预缓存.

  • The service worker first enters the installing phase, and however many install listeners you've registered, they will all get a chance to execute. As you suggest, Workbox creates its own install listener to handle precaching.

只有当每个 install 侦听器都没有错误地完成时,Service Worker 才会进入下一阶段,这可能是 waiting(如果已经有一个使用以前版本的 Service Worker 打开客户端)或激活(如果没有客户端使用以前版本的 Service Worker).

Only if every install listener completes without error will the service worker move on to the next stage, which might either be waiting (if there is already an open client using the previous version of the service worker) or activating (if there are no clients using the previous version of the service worker).

skipWaiting(),如果你选择使用它,它会绕过waiting阶段,无论是否有任何打开的客户端使用之前版本的service worker.

skipWaiting(), if you choose to use it, it will bypass the waiting stage regardless of whether or not there are any open clients using the previous version of the service worker.

如果任何 install 侦听器失败,调用 skipWaiting() 将不会完成任何事情,因为 Service Worker 永远不会离开 installing代码>阶段.这基本上是一个空操作.

Calling skipWaiting() will not accomplish anything if any of the install listeners failed, because the service worker will never leave the installing phase. It's basically a no-op.

当您还使用延迟加载版本化的预缓存资产时,您应该注意的一件事是使用 skipWaiting().正如文章警告的那样:

The one thing that you should be careful about is using skipWaiting() when you are also using lazy-loading of versioned, precached assets. As the article warns:

注意:skipWaiting() 意味着你的新服务工作者很可能控制用旧版本加载的页面.这表示您的一些页面提取将已由您的旧服务处理工作人员,但您的新服务工作人员将在后续处理取.如果这可能会破坏事情,请不要使用 skipWaiting().

Caution: skipWaiting() means that your new service worker is likely controlling pages that were loaded with an older version. This means some of your page's fetches will have been handled by your old service worker, but your new service worker will be handling subsequent fetches. If this might break things, don't use skipWaiting().

由于延迟加载预缓存、版本化资产在 2018 年更为常见,因此 Workbox 默认不会为您调用 skipWaiting().您可以选择是否使用它.

Because lazy-loading precached, versioned assets is a much more common thing to do in 2018, Workbox does not call skipWaiting() for you by default. It's up to you to opt-in to using it.

这篇关于Workbox:self.skipWaiting() 的危险的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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