服务人员将不会安装并保持其状态为冗余。刷新页面将在冗余状态(灰色指示器)中添加更多Service Worker。
我已经在self.addEventListener函数参数中尝试过console.log(“ installed”)并成功了。服务人员被标记为已激活并正在运行(绿色指示灯)
以下将使服务人员变得多余
self.addEventListener("install", function (event) {
event.waitUntil(
caches.open(CACHE_NAME).then(function (cache) {
var newImmutableRequests = [];
return Promise.all(
immutableRequests.map(function (url) {
return caches.match(url).then(function (response) {
if (response) {
return cache.put(url, response);
} else {
newImmutableRequests.push(url);
return Promise.resolve();
}
});
})
).then(function () {
return cache.addAll(newImmutableRequests.concat(mutableRequests));
});
})
);
});
下面的方法可以使用,但不能缓存文件
self.addEventListener("install", function (event) {
console.log("installed");
});
我希望服务工作者可以安装并可以缓存文件,但是它会进入冗余状态。
答案 0 :(得分:0)
您的代码中至少有一个问题:
return caches.match(url).then(function (response) {
应该是cache
,而没有s
,对吗? caches
是全局缓存API接口,而在代码中,cache
是引用单个缓存实例的变量。
此外,要使查找代码中的错误更加容易,请考虑添加错误处理。您的代码中没有任何Promise.catch处理程序,因此它只是吞没了所有错误:)