CacheStorage.delete()

实验性: 这是一项实验性技术
在将其用于生产之前,请仔细检查浏览器兼容性表格

CacheStorage 接口的 delete() 方法查找匹配 cacheNameCache 对象,如果找到,则删除 Cache 对象并返回一个 resolve 为 true 的 Promise . 如果未找到 Cache 对象,则返回 false.

语法

caches.delete(cacheName).then(function(true) {
  //your cache is now deleted
});

Returns

如果找到 Cache 对象,删除它,返回一个 resolve 为 truePromise ,否则,返回 false .

Parameters

cacheName

想要删除的 cache 对象的名称。

实例

在此代码片段中,我们等待一个 activate 事件,然后运行一个 waitUntil() 块,其在一个新的 service worker 被激活前清除所有旧的、未使用的 cache. 这里我们有一个白名单,其中包含我们想要保留的 cache 的 name. 我们使用 CacheStorage.keys 返回 CacheStorage 对象中 cache 的键,然后检查每个键值,以查看它是否在白名单中。如果没有,我们使用 delete() 删除它。

js
this.addEventListener("activate", function (event) {
  var cacheWhitelist = ["v2"];

  event.waitUntil(
    caches.keys().then(function (keyList) {
      return Promise.all(
        keyList.map(function (key) {
          if (cacheWhitelist.indexOf(key) === -1) {
            return caches.delete(key);
          }
        }),
      );
    }),
  );
});

规范

Specification
Service Workers
# cache-storage-delete

浏览器兼容性

BCD tables only load in the browser

See also