The Clients interface provides access to Client objects. Access it via within a service worker.self.clients
The Clients interface provides access to Client objects. Access it via within a service worker.self.clients
Clients.get()Clients.matchAll()Returns a Promise for an array of Client 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 new WindowClient.
Clients.claim()Allows an active service worker to set itself as the controller for all clients within its scope.
The following example shows an existing chat window or creates a new one when the user clicks a notification.
js
addEventListener("notificationclick", (event) => { event.waitUntil( (async () => { 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!"); })(), ); });
| Specification |
|---|
| Service Workers # clients-interface |
| 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 | No | 27 | 11.1 | 40 | 40 | 44 | 27 | 11.3 | 4.0 |
claim |
42 | 17 | 44 | No | 29 | 11.1 | 42 | 42 | 44 | 29 | 11.3 | 4.0 |
get |
51 | 17 | 45 | No | 38 | 11.1 | 51 | 51 | 45 | 41 | 11.3 | 5.0 |
matchAll |
42Client objects returned in most recent focus order. |
17Client objects returned in most recent focus order. |
5444–54Before Firefox 54,Client objects were not returned in the most recent focus order as required by the specification. |
No | 29 | 11.1 | 42Client objects returned in most recent focus order. |
42Client objects returned in most recent focus order. |
5444–54Before Firefox 54,Client objects were not returned in the most recent focus order as required by the specification. |
29 | 11.3 | 4.0Client objects returned in most recent focus order. |
openWindow |
40["Before Chrome 43, this method could only open URLs on the same origin.", "Since Chrome 51, URLs may open inside an existing browsing context provided by a standalone web app."] |
17 | 44 | No | 38 | 11.1 | 40["Before Chrome 43, this method could only open URLs on the same origin.", "Since Chrome 51, URLs may open inside an existing browsing context provided by a standalone web app."] |
40["Before Chrome 43, this method could only open URLs on the same origin.", "Since Chrome 51, URLs may open inside an existing browsing context provided by a standalone web app."] |
44 | 41 | 11.3 | 4.0["Before Samsung Internet 4.0, this method could only open URLs on the same origin.", "Since Samsung Internet 5.0, URLs may open inside an existing browsing context provided by a standalone web app."] |
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Clients