From 074785cea106179cb3305637055ab0a009ca74f2 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:42:52 -0500 Subject: initial commit --- files/ru/web/api/cachestorage/open/index.html | 141 ++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 files/ru/web/api/cachestorage/open/index.html (limited to 'files/ru/web/api/cachestorage/open/index.html') diff --git a/files/ru/web/api/cachestorage/open/index.html b/files/ru/web/api/cachestorage/open/index.html new file mode 100644 index 0000000000..8e7535fdc9 --- /dev/null +++ b/files/ru/web/api/cachestorage/open/index.html @@ -0,0 +1,141 @@ +--- +title: CacheStorage.open() +slug: Web/API/CacheStorage/open +tags: + - API + - CacheStorage + - Experimental + - Method + - Reference + - ServiceWorker +translation_of: Web/API/CacheStorage/open +--- +

{{APIRef("Service Workers API")}}{{SeeCompatTable}}

+ +

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

+ +
+

Note: If the specified {{domxref("Cache")}} does not exist, a new cache is created with that cacheName.

+
+ +

Синтакс

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

Возвращает

+ +

{{jsxref("Promise")}} который резолвится в запрашиваемый {{domxref("Cache")}} обьект.

+ +

Параметры

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

Примеры

+ +

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

+ +
    +
  1. Check whether a match for the request is found in the {{domxref("CacheStorage")}} using {{domxref("CacheStorage.match")}}. If so, serve that.
  2. +
  3. If not, open the v1 cache using {{domxref("CacheStorage.open")}}, put the default network request in the cache using {{domxref("Cache.put")}} and return a clone of the default network request using return response.clone() — necessary because put() consumes the response body.
  4. +
  5. If this fails (e.g., because the network is down), return a fallback response.
  6. +
+ +
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

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Service Workers', '#cache-storage', 'CacheStorage')}}{{Spec2('Service Workers')}}Initial definition.
+ +

Browser compatibility

+ +

{{CompatibilityTable}}

+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatChrome(40.0)}}{{CompatGeckoDesktop(44)}}[1]{{CompatNo}}{{CompatUnknown}}{{CompatNo}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidAndroid WebviewFirefox Mobile (Gecko)IE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatNo}}{{CompatNo}}{{CompatGeckoMobile(44)}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatChrome(40.0)}}
+
+ +

[1] Service workers (and Push) have been disabled in the Firefox 45 Extended Support Release (ESR.)

+ +

See also

+ + -- cgit v1.2.3-54-g00ecf