blob: eee8e0e4435a641f5d4d3ccc67e4511b72a59410 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
---
title: CacheStorage.keys()
slug: Web/API/CacheStorage/keys
translation_of: Web/API/CacheStorage/keys
---
<p><font><font>{{APIRef ("API de Service Workers")}}</font></font></p>
<p><span class="seoSummary"><font><font>El </font></font><code><strong>keys</strong></code><strong><code>()</code></strong><font><font>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. </font><font>Use este método para iterar sobre una lista de todos los objetos {{domxref ("Cache")}}.</font></font></span></p>
<p><font><font>Puede acceder a </font></font><code>CacheStorage</code><font><font>través de la propiedad global {{domxref ("WindowOrWorkerGlobalScope.caches", "caches")}}.</font></font></p>
<h2 id="Sintaxis"><font><font>Sintaxis</font></font></h2>
<pre class="syntaxbox notranslate"><font><font>caches.keys().then(function(</font></font><em><font><font>keyList</font></font></em><font><font>) {</font></font><font><font>
// haz algo con tu keyList</font></font><font><font>
});</font></font>
</pre>
<h3 id="Parámetros"><font><font>Parámetros</font></font></h3>
<p><font><font>Ninguna.</font></font></p>
<h3 id="Valor_de_retorno"><font><font>Valor de retorno</font></font></h3>
<p>a {{jsxref("Promise")}} that resolves with an array of the {{domxref("Cache")}} names inside the {{domxref("CacheStorage")}} object.</p>
<h2 id="Examples" style="line-height: 30px; font-size: 2.14285714285714rem;">Examples</h2>
<p>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 (<code>cacheWhitelist</code>). We return the keys of the caches in the {{domxref("CacheStorage")}} object using <code>keys()</code>, then check each key to see if it is in the whitelist. If not, we delete it using {{domxref("CacheStorage.delete()")}}.</p>
<pre class="brush: js notranslate"><font><font><font><font>then.addEventListener('activar', función (evento) { </font></font></font></font><font><font><font><font>
var cacheWhitelist = ['v2']; </font></font></font></font>
<font><font><font><font>
event.waitUntil( </font></font></font></font><font><font><font><font>
caches.keys().then(function(keyList) { </font></font></font></font><font><font><font><font>
return Promise.all(keyList.map(function(key) {</font></font></font></font><font><font>
if (cacheWhitelist.indexOf(key) === -1) {
return caches.delete(<font><font>key</font></font>);
}
});
})</font></font><font><font>
);</font></font><font><font>
});</font></font></pre>
<h2 id="Especificaciones"><font><font>Especificaciones</font></font></h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col"><font><font>Especificación</font></font></th>
<th scope="col"><font><font>Estado</font></font></th>
<th scope="col"><font><font>Comentario</font></font></th>
</tr>
<tr>
<td><font><font>{{SpecName('Service Workers', '# dom-cachestorage-keys', 'CacheStorage: keys')}}</font></font></td>
<td><font><font>{{Spec2 ('Trabajadores de servicio')}}</font></font></td>
<td><font><font>Definición inicial</font></font></td>
</tr>
</tbody>
</table>
<h2 id="Compatibilidad_del_navegador"><font><font>Compatibilidad del navegador</font></font></h2>
<p><font><font>{{Compat("api.CacheStorage.keys")}}</font></font></p>
<h2 id="Ver_también"><font><font>Ver también</font></font></h2>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker_API/Using_Service_Workers"><font><font>Uso de trabajadores del servicio</font></font></a></li>
<li><font><font>{{domxref("Cache")}}</font></font></li>
<li><font><font><font><font>{{domxref("WindowOrWorkerGlobalScope.caches")}}</font></font></font></font></li>
</ul>
|