caches

全局的 caches 只读属性返回与当前上下文紧密相关的 CacheStorage 对象。此对象激活了诸如存储用于离线使用的资源,并生成对请求的自定义响应等功能。

安全上下文: 此项功能仅在一些支持的浏览器安全上下文(HTTPS)中可用。

一个 CacheStorage 对象。

示例

以下示例展示了你在 service worker 上下文中应该如何运用 cache 对资源进行离线存储。

js
this.addEventListener("install", function (event) {
  event.waitUntil(
    caches.open("v1").then(function (cache) {
      return cache.addAll([
        "/sw-test/",
        "/sw-test/index.html",
        "/sw-test/style.css",
        "/sw-test/app.js",
        "/sw-test/image-list.js",
        "/sw-test/star-wars-logo.jpg",
        "/sw-test/gallery/",
        "/sw-test/gallery/bountyHunters.jpg",
        "/sw-test/gallery/myLittleVader.jpg",
        "/sw-test/gallery/snowTroopers.jpg",
      ]);
    }),
  );
});

规范

Specification
Service Workers
# self-caches

浏览器兼容性

BCD tables only load in the browser

参见