aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/cache/match/index.html
blob: 93cace04e7d6c6686e79c0238e240fe0dc7f45c5 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
---
title: Cache.match()
slug: Web/API/Cache/match
tags:
  - Cache.match()
  - ServiceWorker
  - match
  - 实验性的
translation_of: Web/API/Cache/match
---
<p>{{APIRef("Service Workers API")}}{{SeeCompatTable}}</p>

<p>{{domxref("Cache")}} 接口的 <strong><code>match()</code></strong> 方法, 返回一个 {{jsxref("Promise")}} 解析为(resolve to)与 {{domxref("Cache")}} 对象中的第一个匹配请求相关联的{{domxref("Response")}} 。如果没有找到匹配,{{jsxref("Promise")}} 解析为 {{jsxref("undefined")}}</p>

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

<pre class="brush: js notranslate">cache.match(request,{options}).then(function(response) {
  //操作response
});
</pre>

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

<p>一个 {{jsxref("Promise")}} 对象,该对象解析为第一个匹配请求的 {{domxref("Response")}} 对象,如果没有匹配到,则解析到 {{jsxref("undefined")}}</p>

<div class="note">
<p><strong>Note</strong>: <code>Cache.match()</code> 基本上和 {{domxref("Cache.matchAll()")}} 一样, 只不过 <code>Cache.match()</code> 只解析为 <code>response[0]</code> (第一个匹配的响应(response)对象) 而不是 <code>response[]</code> (所有响应对象组成的数组)。</p>
</div>

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

<dl>
 <dt>request</dt>
 <dd>{{domxref("Cache")}}对象中查找的{{domxref("Request")}}对象对应的response。这个{{domxref("Request")}}可以是 object 或者是一个URL.</dd>
 <dt>options {{optional_inline}}</dt>
 <dd>一个为 <code>match</code> 操作设置选项的对象。有效的选项如下:
 <ul>
  <li><code>ignoreSearch</code>: 一个 {{domxref("Boolean")}} 值用来设置是否忽略url中的query部分。例如, 如果该参数设置为 <code>true</code> ,那么 <code>http://foo.com/?value=bar中的</code> <code>?value=bar</code> 部分就会在匹配中被忽略. 该选项默认为 <code>false</code></li>
  <li><code>ignoreMethod</code>: 一个 {{domxref("Boolean")}} 值,如果设置为 <code>true</code>在匹配时就不会验证 {{domxref("Request")}} 对象的<code>http</code> 方法 (通常只允许是 <code>GET</code> 或 <code>HEAD</code> 。) 该参数默认值为 <code>false</code></li>
  <li><code>ignoreVary</code>: 一个 {{domxref("Boolean")}} 值,该值如果为 <code>true</code> 则匹配时不进行 <code>VARY</code> 部分的匹配。例如,如果一个URL匹配,此时无论{{domxref("Response")}}对象是否包含<code>VARY</code>头部,都会认为是成功匹配。该参数默认为 <code>false</code></li>
  <li><code>cacheName</code>: 一个 {{domxref("DOMString")}} ,代表一个具体的要被搜索的缓存。注意该选项被 <code>Cache.match()</code>方法忽略。</li>
 </ul>
 </dd>
</dl>

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

<p>这个是个来自 <a href="https://github.com/GoogleChrome/samples/blob/gh-pages/service-worker/custom-offline-page/service-worker.js">custom offline page</a> 的例子 (<a href="https://googlechrome.github.io/samples/service-worker/custom-offline-page/index.html">live demo</a>)。</p>

<p>下面的例子在请求失败时提供特定的数据。 <code>catch()</code> 在 <code>fetch()</code> 的调用抛出异常时触发。在 <code>catch()</code> 语句中, <code>match()</code>用来返回正确的响应。</p>

<p>在这个例子中,我们决定只缓存通过GET取得的HTML文档. <span style="line-height: 19.0909080505371px;">如果 <code>if()</code> 条件是 false,那么这个fetch处理器就不会处理这个请求。如果还有其他的fetch处理器被注册,它们将有机会调用 <code>event.respondWith()</code> 如果没有fetch处理器调用 <code>event.respondWith()</code> ,该请求就会像没有 service worker 介入一样由浏览器处理。如果 </span><code style="font-style: normal; line-height: 19.0909080505371px;">fetch()</code><span style="line-height: 19.0909080505371px;"> 返回了有效的HTTP响应,相应码是4xx或5xx,那么</span><code style="font-style: normal; line-height: 19.0909080505371px;">catch()</code><span style="line-height: 19.0909080505371px;"> 就<strong>不会</strong>被调用。</span></p>

<pre class="brush: js notranslate">self.addEventListener('fetch', function(event) {
  // 我们只想在用GET方法请求HTML文档时调用 event.respondWith()。
  if (event.request.method === 'GET' &amp;&amp;
      event.request.headers.get('accept').indexOf('text/html') !== -1) {
    console.log('Handling fetch event for', event.request.url);
    event.respondWith(
      fetch(event.request).catch(function(e) {
        console.error('Fetch failed; returning offline page instead.', e);
        return caches.open(OFFLINE_CACHE).then(function(cache) {
          return cache.match(OFFLINE_URL);
        });
      })
    );
  }
});</pre>

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

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</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.match")}}

<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>