因此此代码将卸载所有服务人员。
How do I uninstall a Service Worker?
navigator.serviceWorker.getRegistrations().then(function(registrations) {
for(let registration of registrations) {
registration.unregister()
} })
然后我有了这段代码,该代码仅针对当前作用域成功注销了服务工作者。
function pwaUninstallServiceWorker() {
return self.registration.unregister()
.then(function(success) {
if (success) {
// Delete all Caches that belong to the PWA module.
caches.keys().then(function(names) {
for (let name of names) {
console.debug('cache name: ', name);
if (name.indexOf(CACHE_PREFIX) !== -1) {
console.debug('PWA: Deleting cache with name ', name);
caches.delete(name);
}
}
console.debug('Service Worker has unregistered itself and destroyed old caches');
});
}
else {
console.error('Service Worker could not unregister itself. It might be necessary to manually delete this Service Worker using browser devtools.');
}
})
.catch(function(error) {
console.error('PWA: Phone-home - ', error);
});
}
有什么我可以运行代码的方式,以便仅当它们关闭站点/范围(与该站点相关的所有选项卡)时,才删除/注销当前作用域的服务工作程序,而不必使用完整的浏览器重新启动?