From 1109132f09d75da9a28b649c7677bb6ce07c40c0 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:41:45 -0500 Subject: initial commit --- files/es/web/api/cachestorage/keys/index.html | 74 +++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 files/es/web/api/cachestorage/keys/index.html (limited to 'files/es/web/api/cachestorage/keys') diff --git a/files/es/web/api/cachestorage/keys/index.html b/files/es/web/api/cachestorage/keys/index.html new file mode 100644 index 0000000000..eee8e0e443 --- /dev/null +++ b/files/es/web/api/cachestorage/keys/index.html @@ -0,0 +1,74 @@ +--- +title: CacheStorage.keys() +slug: Web/API/CacheStorage/keys +translation_of: Web/API/CacheStorage/keys +--- +

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

+ +

El keys()método de la interfaz {{domxref ("CacheStorage")}} devuelve un {{jsxref ("Promise")}} que se resolverá con una matriz que contiene las cadenas correspondientes a todos los {{domxref ("Cache")}} objetos rastreados por el objeto {{domxref ("CacheStorage")}} en el orden en que fueron creados. Use este método para iterar sobre una lista de todos los objetos {{domxref ("Cache")}}.

+ +

Puede acceder a CacheStoragetravés de la propiedad global {{domxref ("WindowOrWorkerGlobalScope.caches", "caches")}}.

+ +

Sintaxis

+ +
caches.keys().then(function(keyList) {
+  // haz algo con tu keyList
+});
+
+ +

Parámetros

+ +

Ninguna.

+ +

Valor de retorno

+ +

a {{jsxref("Promise")}} that resolves with an array of the {{domxref("Cache")}} names inside the {{domxref("CacheStorage")}} object.

+ +

Examples

+ +

In this code snippet we wait for an {{domxref("ServiceWorkerGlobalScope.onactivate", "activate")}} event, and then run a {{domxref("ExtendableEvent.waitUntil","waitUntil()")}} block that clears up any old, unused caches before a new service worker is activated. Here we have a whitelist containing the names of the caches we want to keep (cacheWhitelist). We return the keys of the caches in the {{domxref("CacheStorage")}} object using keys(), then check each key to see if it is in the whitelist. If not, we delete it using {{domxref("CacheStorage.delete()")}}.

+ +
then.addEventListener('activar', función (evento) { 
+  var cacheWhitelist = ['v2']; 
+
+  event.waitUntil( 
+    caches.keys().then(function(keyList) { 
+      return Promise.all(keyList.map(function(key) {
+        if (cacheWhitelist.indexOf(key) === -1) {
+          return caches.delete(key);
+        }
+      });
+    })
+  );
+});
+ +

Especificaciones

+ + + + + + + + + + + + + + +
EspecificaciónEstadoComentario
{{SpecName('Service Workers', '# dom-cachestorage-keys', 'CacheStorage: keys')}}{{Spec2 ('Trabajadores de servicio')}}Definición inicial
+ +

Compatibilidad del navegador

+ + + +

{{Compat("api.CacheStorage.keys")}}

+ +

Ver también

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