--- 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")}} объект.
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:
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.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');
});
| Specification | Status | Comment |
|---|---|---|
| {{SpecName('Service Workers', '#cache-storage', 'CacheStorage')}} | {{Spec2('Service Workers')}} | Initial definition. |
{{Compat}}