This page was translated from English by the community. Learn more and join the MDN Web Docs community.
CacheStorage.open()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since апрель 2018 г..
Экспериментальная возможность: Это экспериментальная технология
Так как спецификация этой технологии ещё не стабилизировалась, смотрите таблицу совместимости по поводу использования в различных браузерах. Также заметьте, что синтаксис и поведение экспериментальной технологии может измениться в будущих версиях браузеров, вслед за изменениями спецификации.
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', ...]
});
Возвращает
Параметры
- 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:
- Check whether a match for the request is found in the
CacheStorageusingCacheStorage.match. If so, serve that. - If not, open the
v1cache usingCacheStorage.open, put the default network request in the cache usingCache.putand return a clone of the default network request usingreturn response.clone()— necessary becauseput()consumes the response body. - 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;
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 |
|---|
| Service Workers Nightly> # cache-storage-open> |
Совместимость с браузерами
Enable JavaScript to view this browser compatibility table.