CacheStorage.open()

Экспериментальная возможность: Это экспериментальная технология
Так как спецификация этой технологии ещё не стабилизировалась, смотрите таблицу совместимости по поводу использования в различных браузерах. Также заметьте, что синтаксис и поведение экспериментальной технологии может измениться в будущих версиях браузеров, вслед за изменениями спецификации.

open() метод из CacheStorage интерфейса возвращает Promise который резолвится в Cache объект с соответствующим cacheName (именем тега кеша).

Примечание: If the specified Cache does not exist, a new cache is created with that cacheName.

Синтаксис

caches.open(cacheName).then(function(cache) {
  //обрабатываем кеш например: cache.AddAll(filesToCache); где filesToCache = ['/mypic.png', ...]
});

Возвращает

Promise который резолвится в запрашиваемый Cache объект.

Параметры

cacheName

Имя (тег) кеша заданное заранее которое необходимо открыть.

Примеры

This code snippet is from the MDN sw-test example (see sw-test running live). Here we wait for a FetchEvent to fire. Then we construct a custom response like so:

  1. Check whether a match for the request is found in the CacheStorage using CacheStorage.match. If so, serve that.
  2. If not, open the v1 cache using CacheStorage.open, put the default network request in the cache using Cache.put (en-US) and return a clone of the default network request using return response.clone() — necessary because put() consumes the response body.
  3. If this fails (e.g., because the network is down), return a fallback response.
js
var response;
var cachedResponse = caches
  .match(event.request)
  .catch(function () {
    return fetch(event.request);
  })
  .then(function (r) {
    response = r;
    caches.open("v1").then(function (cache) {
      cache.put(event.request, response);
    });
    return response.clone();
  })
  .catch(function () {
    return caches.match("/sw-test/gallery/myLittleVader.jpg");
  });

Specifications

Specification
Service Workers
# cache-storage-open

Совместимость с браузерами

BCD tables only load in the browser

See also