ServiceWorkerContainer
The ServiceWorkerContainer
interface of the Service Worker API provides an object representing the service worker as an overall unit in the network ecosystem, including facilities to register, unregister and update service workers, and access the state of service workers and their registrations.
Most importantly, it exposes the ServiceWorkerContainer.register()
method used to register service workers, and the ServiceWorkerContainer.controller
property used to determine whether or not the current page is actively controlled.
Properties
-
ServiceWorkerContainer.controller
Read only -
Returns a
ServiceWorker
object if its state isactivating
oractivated
(the same object returned byServiceWorkerRegistration.active
). This property returnsnull
during a force-refresh request (Shift + refresh) or if there is no active worker. -
ServiceWorkerContainer.ready
Read only -
Provides a way of delaying code execution until a service worker is active. It returns a
Promise
that will never reject, and which waits indefinitely until theServiceWorkerRegistration
associated with the current page has anServiceWorkerRegistration.active
worker. Once that condition is met, it resolves with theServiceWorkerRegistration
.
Events
controllerchange
-
Occurs when the document's associated
ServiceWorkerRegistration
acquires a newactive
worker. Also available via theServiceWorkerContainer.oncontrollerchange
property. error
-
Fired whenever an error occurs in the associated service workers.
message
-
Occurs when incoming messages are received by the
ServiceWorkerContainer
object (e.g. via aMessagePort.postMessage()
call.) Also available via theServiceWorkerContainer.onmessage
property.
Methods
ServiceWorkerContainer.register()
-
Creates or updates a
ServiceWorkerRegistration
for the givenscriptURL
. ServiceWorkerContainer.getRegistration()
-
Gets a
ServiceWorkerRegistration
object whose scope matches the provided document URL. The method returns aPromise
that resolves to aServiceWorkerRegistration
orundefined
. ServiceWorkerContainer.getRegistrations()
-
Returns all
ServiceWorkerRegistration
objects associated with aServiceWorkerContainer
in an array. The method returns aPromise
that resolves to an array ofServiceWorkerRegistration
. ServiceWorkerContainer.startMessages()
-
explicitly starts the flow of messages being dispatched from a service worker to pages under its control (e.g. sent via
Client.postMessage()
). This can be used to react to sent messages earlier, even before that page's content has finished loading.
Examples
The example below first checks to see if the browser supports service workers. If supported, the code registers the service worker and determines if the page is actively controlled by the service worker. If it isn't, it prompts the user to reload the page so the service worker can take control. The code also reports any registration failures.
if ('serviceWorker' in navigator) { // Register a service worker hosted at the root of the // site using the default scope. navigator.serviceWorker.register('/sw.js').then(function(registration) { console.log('Service worker registration succeeded:', registration); // At this point, you can optionally do something // with registration. See https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration }).catch(function(error) { console.log('Service worker registration failed:', error); }); // Independent of the registration, let's also display // information about whether the current page is controlled // by an existing service worker, and when that // controller changes. // First, do a one-off check if there's currently a // service worker in control. if (navigator.serviceWorker.controller) { console.log('This page is currently controlled by:', navigator.serviceWorker.controller); } // Then, register a handler to detect when a new or // updated service worker takes control. navigator.serviceWorker.oncontrollerchange = function() { console.log('This page is now controlled by:', navigator.serviceWorker.controller); }; } else { console.log('Service workers are not supported.'); }
Specifications
Specification |
---|
Service Workers 1 # serviceworkercontainer-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 | |
ServiceWorkerContainer |
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 |
controller |
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 |
getRegistration |
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 |
getRegistrations |
45 |
17 |
44
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
|
No |
27 |
11.1 |
40 |
45 |
44 |
27 |
11.3 |
4.0 |
message_event |
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 |
oncontrollerchange |
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 |
onerror |
No |
No |
44
See bug 1714533.
|
No |
No |
No |
No |
No |
44
See bug 1714533.
|
No |
No |
No |
onmessage |
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 |
onmessageerror |
No |
17-79 |
65 |
No |
? |
11.1
Although the
onmessageerror property is supported, the messageerror event is never fired. See bug 171216. |
No |
No |
65 |
? |
11.3
Although the
onmessageerror property is supported, the messageerror event is never fired. See bug 171216. |
No |
ready |
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 |
register |
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 |
startMessages |
74 |
≤79 |
64 |
No |
62 |
11.1 |
74 |
74 |
64 |
50 |
11.3 |
11.0 |
See also
- Using Service Workers
- Service workers basic code example
- Is ServiceWorker ready?
Promise
- Using web workers
© 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/ServiceWorkerContainer