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

Метод put() интерфейса Cache позволяет добавлять пары ключ/значение в текущий объект Cache.

Often, you will just want to fetch() one or more requests, then add the result straight to your cache. In such cases you are better off using Cache.add()/Cache.addAll(), as they are shorthand functions for one or more of these operations.

fetch(url).then(function(response) {
  if (!response.ok) {
    throw new TypeError('Bad response status');
  return cache.put(url, response);

Примечание: put() will overwrite any key/value pair previously stored in the cache that matches the request.

Примечание: Cache.add/Cache.addAll do not cache responses with Response.status values that are not in the 200 range, whereas Cache.put lets you store any request/response pair. As a result, Cache.add/Cache.addAll can't be used to store opaque responses, whereas Cache.put can.

Примечание: Initial Cache implementations (in both Blink and Gecko) resolve Cache.add, Cache.addAll, and Cache.put promises when the response body is fully written to the disk. More recent spec versions state 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.


cache.put(request, response).then(function() {
  // request/response pair has been added to the cache



The Request you want to add to the cache.


The Response you want to match up to the request.

Return value

A Promise that resolves with void.

Примечание: The promise will reject with a TypeError if the URL scheme is not http or https.


This example is from the MDN sw-test example (see sw-test running live). Here we wait for a FetchEvent to fire. 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 open(), put the default network request in the cache using Cache.put() and return a clone of the default network request using return response.clone(). Clone is needed because put() consumes the response body.
  3. If this fails (e.g., because the network is down), return a fallback response.
var response;
var cachedResponse = caches.match(event.request).catch(function() {
  return fetch(event.request);
}).then(function(r) {
  response = r;'v1').then(function(cache) {
    cache.put(event.request, response);
  return response.clone();
}).catch(function() {
  return caches.match('/sw-test/gallery/myLittleVader.jpg');


Specification Status Comment
Service Workers
Определение 'Cache: put' в этой спецификации.
Рабочий черновик Initial definition.

Browser compatibility

BCD tables only load in the browser

See also