This feature is well established and works across many devices and browser versions. It’s been available across browsers since April 2018.
* Some parts of this feature may have varying levels of support.
Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
Note: This feature is available in Web Workers.
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.
ServiceWorkerContainer objects are exposed in the Window scope through Navigator.serviceWorker and in workers using WorkerNavigator.serviceWorker (if supported — check browser compatibility).
ServiceWorkerContainer.controller Read only
A ServiceWorker object that represents the active service worker controlling the current page or null if the page has no active or activating service worker.
ServiceWorkerContainer.ready Read only
Returns a Promise that resolves with the ServiceWorkerRegistration associated with the current page, but only when there is an active service worker. This provides a mechanism to defer code execution until a service worker is active.
ServiceWorkerContainer.getRegistration()Gets a ServiceWorkerRegistration object whose scope matches the provided document URL. The method returns a Promise that resolves to a ServiceWorkerRegistration or undefined.
ServiceWorkerContainer.getRegistrations()Returns all ServiceWorkerRegistration objects associated with a ServiceWorkerContainer in an array. The method returns a Promise that resolves to an array of ServiceWorkerRegistration.
ServiceWorkerContainer.register()Creates or updates a ServiceWorkerRegistration for the given scriptURL.
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.
controllerchangeFired when the document's associated ServiceWorkerRegistration acquires a new active worker.
messageFired when incoming messages are received by the ServiceWorkerContainer object (e.g., via a MessagePort.postMessage() call).
messageerrorFired when incoming messages can not deserialized by the ServiceWorkerContainer object (e.g., via a MessagePort.postMessage() call).
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((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((error) => {
console.error(`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 = () => {
console.log(
"This page is now controlled by",
navigator.serviceWorker.controller,
);
};
} else {
console.log("Service workers are not supported.");
}
| Specification |
|---|
| Service Workers> # serviceworkercontainer-interface> |
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | WebView on iOS | |
ServiceWorkerContainer |
40 | 17 | 44 | 27 | 11.1 | 40 | 44 | 27 | 11.3 | 4.0 | 40 | No |
controller |
40 | 17 | 44 | 27 | 11.1 | 40 | 44 | 27 | 11.3 | 4.0 | 40 | No |
controllerchange_event |
40 | 17 | 44 | 27 | 11.1 | 40 | 44 | 27 | 11.3 | 4.0 | 40 | No |
getRegistration |
40 | 17 | 44 | 27 | 11.1 | 40 | 44 | 27 | 11.3 | 4.0 | 40 | No |
getRegistrations |
45 | 17 | 44 | 27 | 11.1 | 45 | 44 | 27 | 11.3 | 4.0 | 40 | No |
message_event |
40 | 17 | 44 | 27 | 11.1 | 40 | 44 | 27 | 11.3 | 4.0 | 40 | No |
messageerror_event |
80 | 8017–79 | 65 | 67 | 16.411.1–16.4Although theonmessageerror event handler property is supported, the messageerror event is never fired. |
80 | 65 | 57 | 16.411.3–16.4Although theonmessageerror event handler property is supported, the messageerror event is never fired. |
13.0 | 80 | No |
ready |
40 | 17 | 44 | 27 | 11.1 | 40 | 44 | 27 | 11.3 | 4.0 | 40 | No |
register |
40 | 17 | 44 | 27 | 11.1 | 40 | 44 | 27 | 11.3 | 4.0 | 40 | No |
startMessages |
74 | 79 | 64 | 62 | 11.1 | 74 | 64 | 50 | 11.3 | 11.0 | 74 | No |
worker_support |
No | No | 133 | No | 11.1 | No | 133 | No | 11.3 | No | No | No |
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer