javascript - 是否可以从新的服务人员中检测到现有服务人员的存在?
问题描述
我有一个复杂的服务人员。在安装时,它的初始设置行为需要根据是否已经有另一个服务工作者为源站准备就绪而改变。
是否可以从服务人员内部(最好是在install
事件期间)检测另一个服务人员是否已经处于活动状态?
从页面上下文中看起来我可以使用navigator.serviceWorker.getRegistrations()
,但navigator.serviceWorker
在服务工作者本身内部是未定义的。
解决方案
假设您没有在部署之间更改服务工作者的 URL 或范围,您应该能够从self.registration
.
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.
});
推荐阅读
- sql - 如何组合 SUM(Cases) 和多个表
- pom.xml - 使用 jenkins 将 java 合同发布到协议流时出现未经授权的错误
- docker - 如何在没有“服务容器”的情况下在 Github Workflow 中使用 docker-compose/docker 连接到服务?
- sumologic - 如何计算和比较正则表达式匹配的数量
- php - MAMP 不启动任何服务器
- django - 使用 Nginx 的 Django 主机
- i3 - i3/sway - 如何绑定符号减号和等号
- java - getOutputStream 调用后连接重置
- spring - 如何让浏览器知道从spring服务器下载过程中发生了错误?
- metadata - Ag-grid 列定义和 `__metadata__` 属性