blob: 4797f8c064e81c780f38e55d7a1ba591b2c23bd1 (
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
|
---
title: Clients.claim()
slug: Web/API/Clients/claim
translation_of: Web/API/Clients/claim
---
<p>{{SeeCompatTable}}{{APIRef("Service Worker Clients")}}</p>
<p><span class="seoSummary">{{domxref("Clients")}} 接口的 <strong><code>claim()</code></strong> 方法允许一个激活的 service worker 将自己设置为其 {{domxref("ServiceWorkerRegistration.scope", "scope")}} 内所有</span>clients 的<span class="seoSummary">{{domxref("ServiceWorkerContainer.controller", "controller")}} . 这会在由此service worker 控制的任何 clients 中触发 {{domxref("ServiceWorkerContainer","navigator.serviceWorker")}} 上的 "<code>controllerchange</code>" 事件.</span></p>
<p>当一个 service worker 被初始注册时,页面在下次加载之前不会使用它。 <code>claim()</code> 方法会立即控制这些页面。请注意,这会导致 service worker 控制通过网络定期加载的页面,或者可能通过不同的 service worker 加载.</p>
<h2 id="语法">语法</h2>
<pre class="syntaxbox">await clients.claim();
</pre>
<h3 id="参数">参数</h3>
<p>None.</p>
<h3 id="返回">返回</h3>
<p>A {{jsxref("Promise")}} for <code>void</code>.</p>
<h2 id="示例">示例</h2>
<p>The following example uses <code>claim()</code> inside service worker's "<code>activate</code>" event listener so that clients loaded in the same scope do not need to be reloaded before their fetches will go through this service worker.</p>
<pre class="brush: js">self.addEventListener('activate', event => {
event.waitUntil(clients.claim());
});</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', '#clients-claim', 'claim()')}}</td>
<td>{{Spec2('Service Workers')}}</td>
<td>Initial definition.</td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<div>
<p>{{Compat("api.Clients.claim")}}</p>
</div>
<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://developers.google.com/web/fundamentals/instant-and-offline/service-worker/lifecycle">The service worker lifecycle</a></li>
<li><a href="https://jakearchibald.github.io/isserviceworkerready/">Is ServiceWorker ready?</a></li>
<li>{{jsxref("Promise", "Promises")}}</li>
<li>{{domxref("ServiceWorkerGlobalScope.skipWaiting()", "self.skipWaiting()")}} - skip the service worker's waiting phase </li>
</ul>
|