[英]service worker showing as deleted even though registration was successful
I can't seem to figure out any reason why a service worker would be deleted with the code that I have that registers or actually is the service worker. 我似乎无法弄清楚为什么服务工作者会被我注册的代码或实际上是服务工作者的代码删除。
But in this site, it shows up as deleted in the Service Workers section of the chrome dev tools (image below). 但在此站点中,它在chrome dev工具的Service Workers部分中显示为已删除 (下图)。
Yet it is also registering properly as logged in the console (same image below). 然而,它也在登录控制台时正确注册(下图中相同)。
Here is the service worker registration code: 这是服务工作者注册码:
if('serviceWorker' in navigator){
navigator.serviceWorker.register('/earnie.min.js', { scope: '/'}).then(function(registration){
console.log('Registration successful, scope is:', registration.scope);
}).catch(function(error){
console.log('Service worker registration failed, error:', error);
});
}
Here is the service worker code: 这是服务工作者代码:
var cachename="e2",cachelist=";;.;/;./;/privacyPolicy.html;/css/main.css;/css/normalize.css;".split(";");
self.addEventListener("install",function(a){
a.waitUntil(caches.open(cachename).then(function(a){
return a.addAll(cachelist)
}))
});
self.addEventListener("fetch",function(a){
a.respondWith(caches.match(a.request).then(function(b){
return b?b:fetch(a.request.clone(), { credentials: 'include', redirect: 'follow' })
}))
});
What is causing it to be deleted and not registering? 是什么导致它被删除而没有注册?
Registration succeeded, but installation actually fails. 注册成功,但安装实际上失败了。 Your
waitUntil()
promise is not resolving, which causes InstallEvent
event to fail, thus deleting the ServiceWorker. 您的
waitUntil()
承诺未解析,这会导致InstallEvent
事件失败,从而删除ServiceWorker。 cachelist
probably returns invalid/empty values when you run split(';')
运行
split(';')
时, cachelist
可能会返回无效/空值
I recommend ensuring that cachelist
is an array with valid URI values, then you can debug within the install event** 我建议确保
cachelist
是一个具有有效URI值的数组,然后你可以在install事件中调试**
self.addEventListener("install", event => {
event.waitUntil(
caches.open(cachename)
.then(cache => cache.addAll(cachelist))
.catch(error => console.error('💩', error))
)
})
**You'll most likely need "Preserve log" console option enabled in Chrome Dev Tools to see the console error. **您最有可能在Chrome开发工具中启用“保留日志”控制台选项以查看控制台错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.