diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/serviceworkerregistration/update | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/api/serviceworkerregistration/update')
-rw-r--r-- | files/zh-cn/web/api/serviceworkerregistration/update/index.html | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/serviceworkerregistration/update/index.html b/files/zh-cn/web/api/serviceworkerregistration/update/index.html new file mode 100644 index 0000000000..aa89f12d69 --- /dev/null +++ b/files/zh-cn/web/api/serviceworkerregistration/update/index.html @@ -0,0 +1,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> |