Movatterモバイル変換


[0]ホーム

URL:


  1. 面向开发者的 Web 技术
  2. Web API
  3. Cache
  4. Cache.addAll()

此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in EnglishAlways switch to English

Cache.addAll()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2018年4月⁩.

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

概要

Cache 接口的addAll() 方法接受一个 URL 数组,检索它们,并将生成的 response 对象添加到给定的缓存中。在检索期间创建的 request 对象成为存储的 response 操作的 key。

备注:addAll() will overwrite any key/value pairs previously stored in the cache that match the request, but will fail if a resultingput() operation would overwrite a previous cache entry created by the sameaddAll() method.

备注:Initial Cache implementations (in both Blink and Gecko) resolveCache.add,Cache.addAll, andCache.put promises when the response body is fully written to storage. More recent spec versions have newer language stating that the browser can resolve the promise as soon as the entry is recorded in the database even if the response body is still streaming in.

语法

js
cache.addAll(requests[]).then(function() {  //requests have been added to the cache});

参数

requests

要获取并添加到缓存的字符串 URL 数组。

返回值

APromise that resolves with void.

Exceptions

ExceptionHappens when
TypeErrorThe URL scheme is nothttp orhttps.The Response status is not in the 200 range (i.e., not a successful response.) This occurs if the request does not return successfully, but also if the request is across-origin no-cors request (in which case the reported status is always 0.)

示例

此代码块等待一个InstallEvent 事件触发,然后运行waitUntil 来处理该应用程序的安装进程。包括调用CacheStorage.open 创建一个新的 cache,然后使用addAll() 添加一系列资源。

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 Nightly
# cache-addAll

浏览器兼容性

参见

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp