aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/cache/keys/index.html
blob: e1ba2b8da191df74084094687a0a7a8967ff5fd6 (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
75
76
77
78
79
80
---
title: Cache.keys()
slug: Web/API/Cache/keys
translation_of: Web/API/Cache/keys
---
<p>{{APIRef("Service Workers API")}}{{SeeCompatTable}}</p>

<p> {{domxref("Cache")}} 接口的 <strong><code>keys()</code></strong> 方法返回一个 {{jsxref("Promise")}} ,这个 {{jsxref("Promise")}} 将解析为一个{{domxref("Cache")}} 键的数组。</p>

<p>请求将以它们被插入的顺序返回。</p>

<div class="note">
<p><strong>注意</strong>: 具有相同URL但不同请求头的请求,如果它们的响应头中有 VARY 头部,则他们可以被返回。</p>
</div>

<h2 id="语法">语法</h2>

<pre class="brush: js">cache.keys(request,{options}).then(function(keys) {
  //do something with your array of requests
});
</pre>

<h3 id="返回值">返回值</h3>

<p>返回一个解析为 {{domxref("Cache")}} 键数组的 {{jsxref("Promise")}}</p>

<h3 id="参数">参数</h3>

<dl>
 <dt>request {{optional_inline}}</dt>
 <dd>如果一个相关键被指定,则返对应的 {{domxref("Request")}}</dd>
 <dt>options {{optional_inline}}</dt>
 <dd>一个对象,它的属性决定了 keys 操作中的匹配操作是如何执行的。可选的属性有:
 <ul>
  <li><code>ignoreSearch</code>: 一个 {{domxref("Boolean")}} 值,指定了匹配操作是否忽略url中的查询部分。如果为 true ,在执行匹配操作时, <code>http://foo.com/?value=bar</code> 的 <code>?value=bar 部分将会被忽。默认为 </code><code>false 。</code></li>
  <li><code>ignoreMethod</code>: 一个 {{domxref("Boolean")}} 值,当为 true 时, 将会阻止匹配操作验证 {{domxref("Request")}} 的 HTTP 方法(通常只有 GET 和 HEAD 方法被允许)。默认为 false 。</li>
  <li><code>ignoreVary</code>: 一个 {{domxref("Boolean")}} 值,当为 <code>true 时,告诉匹配操作不要验证 VARY 头部。换句话说,如果 URL 匹配,你会得到一个匹配而不管</code> {{domxref("Response")}} 对象是否有 VARY 头部。默认为 false 。</li>
  <li><code>cacheName</code>: 一个 {{domxref("DOMString")}} 值,描述了在一个特定的 cache 中进行匹配。注意这个选项会被 Cache.keys()方法忽略。</li>
 </ul>
 </dd>
</dl>

<h2 id="示例" style="line-height: 30px; font-size: 2.14285714285714rem;">示例</h2>

<pre class="brush: js">caches.open('v1').then(function(cache) {
  cache.keys().then(function(keys) {
    keys.forEach(function(request, index, array) {
      cache.delete(request);
    });
  });
})</pre>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">规范</th>
   <th scope="col">状态</th>
   <th scope="col">备注</th>
  </tr>
  <tr>
   <td>{{SpecName('Service Workers', '#cache', 'Cache')}}</td>
   <td>{{Spec2('Service Workers')}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

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

<h2 id="参见">参见</h2>

<ul>
 <li><a href="https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker_API/Using_Service_Workers">Using Service Workers</a></li>
 <li>{{domxref("Cache")}}</li>
 <li>{{domxref("WorkerGlobalScope.caches")}}</li>
</ul>