我有一个复杂的服务人员。当它安装时,它的初始设置行为需要改变,这取决于是否已经有另一个服务工作者已经就位。
是否可以从服务工作者内部检测另一个服务工作者是否已经处于活动状态,最好是在install事件期间?
从页面上下文中看,我可以使用navigator.serviceWorker.getRegistrations(),但是navigator.serviceWorker在服务工作者本身内部是未定义的。
发布于 2020-10-07 23:07:34
假设您在两次部署之间不更改服务工作者的网址或范围,您应该能够从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.
});https://stackoverflow.com/questions/64224122
复制相似问题