[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ru/docs/Web/API/Cache [Back]  [Original]

Cache - API | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Cache

Baseline
Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2018 ..

Cache Request / Response, , , ServiceWorker. , Cache , . , .

Cache. , (, ServiceWorker) Cache. Cache , ; . CacheStorage.open(cacheName), Cache Cache .

. , . , , . , , . , . .

: VARY . , , Cache.

: API HTTP.

In this article

Cache.match(request, options)

Promise, Cache.

Cache.matchAll(request, options)

Promise, Cache.

Cache.add(request)

URL, . fetch() Cache.put() .

Cache.addAll(requests)

URL , , .

Cache.put(request, response)

.

Cache.delete(request, options)

Cache, , , , Cache Promise, true. Cache , false.

Cache.keys(request, options)

Promise, Cache.

. ( ). CacheStorage.open(cacheName) Cache Content-Type, font/.

Cache.match(request, options) , , , , . , Cache.put(request, response) .

, fetch(). , HTTP- (, 404) . .

, . , . , . , CURRENT_CACHES.

"" WorkerGlobalScope . CacheStorage, CacheStorage API.

: Chrome, chrome://inspect/#service-workers "inspect" service-worker.js.

js
var CACHE_VERSION = 1;

//       .
var CURRENT_CACHES = {
  font: "font-cache-v" + CACHE_VERSION,
};

self.addEventListener("activate", function (event) {
  var expectedCacheNames = Object.keys(CURRENT_CACHES).map(function (key) {
    return CURRENT_CACHES[key];
  });

  //       ,  promise   .
  event.waitUntil(
    caches.keys().then(function (cacheNames) {
      return Promise.all(
        cacheNames.map(function (cacheName) {
          if (expectedCacheNames.indexOf(cacheName) == -1) {
            console.log("Deleting out of date cache:", cacheName);

            return caches.delete(cacheName);
          }
        }),
      );
    }),
  );
});

self.addEventListener("fetch", function (event) {
  console.log("Handling fetch event for", event.request.url);

  event.respondWith(
    //   Cache,   'font'.
    caches.open(CURRENT_CACHES["font"]).then(function (cache) {
      return cache
        .match(event.request)
        .then(function (response) {
          if (response) {
            console.log("Found response in cache:", response);

            return response;
          }
        })
        .catch(function (error) {
          //    match()  fetch().
          console.error("Error in fetch handler:", error);

          throw error;
        });
    }),
  );
});

Specification
Service Workers Nightly
# cache-interface


Web Proxy Viewer  |  New URL  |  Original Page