The Clients
interface provides access to Client
objects. Access it via
within a service worker.self
.clients
Clients
Methods
-
Clients.get()
-
Clients.matchAll()
-
Returns a
Promise
for an array ofClient
objects. An options argument allows you to control the types of clients returned. -
Clients.openWindow()
-
Opens a new browser window for a given URL and returns a
Promise
for the newWindowClient
. -
Clients.claim()
-
Allows an active service worker to set itself as the
controller
for all clients within itsscope
.
Examples
The following example shows an existing chat window or creates a new one when the user clicks a notification.
addEventListener('notificationclick', event => { event.waitUntil(async function() { const allClients = await clients.matchAll({ includeUncontrolled: true }); let chatClient; // Let's see if we already have a chat window open: for (const client of allClients) { const url = new URL(client.url); if (url.pathname == '/chat/') { // Excellent, let's use it! client.focus(); chatClient = client; break; } } // If we didn't find an existing chat window, // open a new one: if (!chatClient) { chatClient = await clients.openWindow('/chat/'); } // Message the client: chatClient.postMessage("New chat messages!"); }()); });
Specifications
Specification |
---|
Service Workers 1 # clients-interface |
Browser compatibility
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
Clients |
40
|
17
|
44
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
|
No
|
27
|
11.1
|
40
|
40
|
44
|
27
|
11.3
|
4.0
|
claim |
42
|
17
|
44
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
|
No
|
29
|
11.1
|
42
|
42
|
44
|
29
|
11.3
|
4.0
|
get |
51
|
17
|
45
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
|
No
|
38
|
11.1
|
51
|
51
|
45
|
41
|
11.3
|
5.0
|
matchAll |
42
Client objects returned in most recent focus order.
|
17
Client objects returned in most recent focus order.
|
54
Client objects returned in most recent focus order.
44
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
|
No
|
29
|
11.1
|
42
Client objects returned in most recent focus order.
|
42
Client objects returned in most recent focus order.
|
54
Client objects returned in most recent focus order.
44
|
29
|
11.3
|
4.0
Client objects returned in most recent focus order.
|
openWindow |
51
URLs may open inside an existing browsing context provided by a standalone web app
43
Can open any URL.
42
Can only open URLs on the same origin.
40
|
79
URLs may open inside an existing browsing context provided by a standalone web app
17
|
45
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
|
No
|
38
|
11.1
|
51
URLs may open inside an existing browsing context provided by a standalone web app
43
Can open any URL.
42
Can only open URLs on the same origin.
40
|
51
URLs may open inside an existing browsing context provided by a standalone web app
43
Can open any URL.
42
Can only open URLs on the same origin.
40
|
45
|
41
|
11.3
|
5.0
URLs may open inside an existing browsing context provided by a standalone web app
4.0
|
See also
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Clients