首页 > 解决方案 > Deleting Service worker cache crashes

问题描述

I have added the following (sample) code to my app (Angular app, is true for any JS app) to delete the serviceworker cache:

if ('serviceWorker' in navigator) {
    if ('caches' in window) {
        caches.keys().then((keyList) => {
            return Promise.all(keyList.map(function (key) {
                return caches.delete(key);
            }));
        });
    }
}

But I frequently receive crash reports with the following error for some users using Chrome 72 and above for Windows:

TypeError · undefined is not a function

for function

caches.keys

Anyone has any idea why this could be happening.

标签: javascriptservice-workerangular-service-worker

解决方案


keys 方法只能在 Cache objects 上找到。

您可以从cache.open函数中获取缓存对象。

示例用法:

caches.open(name).then(function(cache) {
  cache.keys().then(function(keys) {
    //Do something with keys
  });
})

推荐阅读