首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否可以从新的服务工作者中检测现有服务工作者的存在?

是否可以从新的服务工作者中检测现有服务工作者的存在?
EN

Stack Overflow用户
提问于 2020-10-06 18:39:31
回答 1查看 59关注 0票数 0

我有一个复杂的服务人员。当它安装时,它的初始设置行为需要改变,这取决于是否已经有另一个服务工作者已经就位。

是否可以从服务工作者内部检测另一个服务工作者是否已经处于活动状态,最好是在install事件期间?

从页面上下文中看,我可以使用navigator.serviceWorker.getRegistrations(),但是navigator.serviceWorker在服务工作者本身内部是未定义的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-07 23:07:34

假设您在两次部署之间不更改服务工作者的网址或范围,您应该能够从self.registration获得该信息。

代码语言:javascript
复制
self.addEventListener('install', (event) => {
  const {installing, waiting, active} = self.registration;

  // Since we're in the install handler, `installing` should
  // be set to the SW whose global state is `self`.

  // `waiting` is likely to be `null`, unless there happened to be a SW
  // that was waiting to activate at the point this SW started install.

  // `active` is what you're most interested in. If it's null, then there
  // isn't already an active SW with the same registration info.
  // If it's set to a SW, then you know that there is one.
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64224122

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档