CacheStorage.open()
Experimental: Это экспериментальная технология
Так как спецификация этой технологии ещё не стабилизировалась, смотрите таблицу совместимости по поводу использования в различных браузерах. Также заметьте, что синтаксис и поведение экспериментальной технологии может измениться в будущих версиях браузеров, вслед за изменениями спецификации.
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
CacheStorage
usingCacheStorage.match
. If so, serve that. - If not, open the
v1
cache usingCacheStorage.open
, put the default network request in the cache usingCache.put
and 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');
});
Specifications
Specification | Status | Comment |
---|---|---|
Service Workers Определение 'CacheStorage' в этой спецификации. |
Рабочий черновик | Initial definition. |
Browser compatibility
BCD tables only load in the browser