blob: aa89f12d69a776858bb26e0eaaf4fafa142eb4f0 (
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
|
---
title: ServiceWorkerRegistration.update()
slug: Web/API/ServiceWorkerRegistration/update
tags:
- 方法
- 更新
translation_of: Web/API/ServiceWorkerRegistration/update
---
<div>{{APIRef("Service Workers API")}}</div>
<div>{{domxref("ServiceWorkerRegistration")}} 的<code><strong>update</strong></code>方法尝试更新service worker。获得worker脚本的URL,逐字节匹配新获取的worker和当前的worker,存在差异的时候安装新的worker。获取worker脚本的更新操作会忽略浏览器缓存的24小时前的内容。</div>
<div class="note">
<p><strong>注意</strong>: 这个特性也应用于 <a href="/en-US/docs/Web/API/Web_Workers_API">Web Workers</a>.</p>
</div>
<h2 id="Syntax" name="Syntax" style="line-height: 30px; font-size: 2.14285714285714rem;">语法</h2>
<pre>ServiceWorkerRegistration.update();</pre>
<h3 id="参数">参数</h3>
<p>None.</p>
<h3 id="返回">返回</h3>
<p>返回 {{domxref("Promise")}} 在resolve时对应一个 {{domxref("ServiceWorkerRegistration")}} 对象。</p>
<h2 id="示例">示例</h2>
<p>下面的示例注册一个service worker,然后绑定事件到按钮,这样你可以有需要时,明确的更新server worker:</p>
<pre class="brush: js">if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/sw-test/sw.js', {scope: 'sw-test'}).then(function(registration) {
// registration worked
console.log('Registration succeeded.');
button.onclick = function() {
registration.update();
}
}).catch(function(error) {
// registration failed
console.log('Registration failed with ' + error);
});
};</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', '#service-worker-registration-update-method', 'ServiceWorkerRegistration.update()')}}</td>
<td>{{Spec2('Service Workers')}}</td>
<td>Initial definition.</td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{Compat("api.ServiceWorkerRegistration.update")}}</p>
<h2 id="See_also">See also</h2>
<ul>
<li><a href="/en-US/docs/Web/API/ServiceWorker_API/Using_Service_Workers">Using Service Workers</a></li>
<li><a href="https://github.com/mdn/sw-test">Service workers basic code example</a></li>
<li><a href="https://jakearchibald.github.io/isserviceworkerready/">Is ServiceWorker ready?</a></li>
<li>{{jsxref("Promise")}}</li>
<li><a href="/en-US/docs/Web/Guide/Performance/Using_web_workers">Using web workers</a></li>
</ul>
|