服务人员被标记为冗余

时间:2019-07-08 08:59:26

标签: javascript promise service-worker service-worker-events

服务人员将不会安装并保持其状态为冗余。刷新页面将在冗余状态(灰色指示器)中添加更多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");
});

我希望服务工作者可以安装并可以缓存文件,但是它会进入冗余状态。

1 个答案:

答案 0 :(得分:0)

您的代码中至少有一个问题:

return caches.match(url).then(function (response) {

应该是cache,而没有s,对吗? caches是全局缓存API接口,而在代码中,cache是引用单个缓存实例的变量。

此外,要使查找代码中的错误更加容易,请考虑添加错误处理。您的代码中没有任何Promise.catch处理程序,因此它只是吞没了所有错误:)

相关问题