blob: cd2b4f46b0b7cd9a3e281f422a23e9477ee729a5 (
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
|
---
title: CacheStorage.open()
slug: Web/API/CacheStorage/open
translation_of: Web/API/CacheStorage/open
---
<p>{{APIRef("Service Workers API")}}{{SeeCompatTable}}</p>
<p><span class="seoSummary">{{domxref("CacheStorage")}} </span>接口的<span class="seoSummary"> <strong><code>open()</code></strong> 方法返回一个resolve为匹配 <code>cacheName</code> 的 {{domxref("Cache")}} 对象的 {{jsxref("Promise")}} .</span></p>
<div class="note">
<p><strong>注意</strong>: 如果指定的 {{domxref("Cache")}} 不存在,则使用该 <code>cacheName</code> 创建一个新的cache,并返回一个resolve为该新 {{domxref("Cache")}} 对象的{{jsxref("Promise")}}.</p>
</div>
<h2 id="语法">语法</h2>
<pre class="brush: js">// "caches" is a global read-only variable, which is an instance of CacheStorage,
// For more info, refer to: https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/caches
caches.open(<em>cacheName</em>).then(function(<em>cache</em>) {
// Do something with your cache
});
</pre>
<h3 id="参数">参数</h3>
<dl>
<dt>cacheName</dt>
<dd>要打开的cache对象name.</dd>
</dl>
<h3 id="返回值">返回值</h3>
<p>一个resolve为请求的 {{domxref("Cache")}} 对象的 {{jsxref("Promise")}} .</p>
<h2 id="示例" style="line-height: 30px; font-size: 2.14285714285714rem;">示例</h2>
<p>此示例来自于MDN <a href="https://github.com/mdn/sw-test/">sw-test example</a> (请参阅 <a href="https://mdn.github.io/sw-test/">sw-test running live</a>)。这里,等待 {{domxref("FetchEvent")}} 事件触发。我们构建自定义响应,像这样:</p>
<ol>
<li>使用 {{domxref("CacheStorage.match","CacheStorage.match()")}} 检查 {{domxref("CacheStorage")}} 中是否存在匹配请求,如果存在,则使用它。</li>
<li>如果没有,使用 {{domxref("CacheStorage.open","CacheStorage.open()")}} 打开 <code>v1</code> cache,使用 {{domxref("Cache.put","Cache.put()")}} 将默认网络请求放入 cache 中,并使用 <code>return response.clone()</code> 返回默认网络请求的克隆副本。最后一个是必须的,因为 <code>put()</code> 使用响应主体。</li>
<li>如果此操作失败(例如,因为网络已关闭),则返回备用响应。</li>
</ol>
<pre class="brush: js">var cachedResponse = caches.match(event.request).catch(function() {
return fetch(event.request);
}).then(function(response) {
caches.open('v1').then(function(cache) {
cache.put(event.request, response);
});
return response.clone();
}).catch(function() {
return caches.match('/sw-test/gallery/myLittleVader.jpg');
});</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-storage', 'CacheStorage')}}</td>
<td>{{Spec2('Service Workers')}}</td>
<td>Initial definition.</td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<p>{{Compat("api.CacheStorage.open")}}</p>
<h2 id="See_also">See also</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>
|