This is an experimental technology
Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future versions of browsers as the specification changes.
The ServiceWorkerContainer interface of the ServiceWorker 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(scriptURL, scope[, base]) 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.controllerRead only- Returns a
ServiceWorkerobject if its state isactivated(the same object returned byServiceWorkerRegistration.active). This property returnsnullif the request is a force refresh (Shift + refresh) or if there is no active worker.
ServiceWorkerContainer.readyRead only- Defines whether a service worker is ready to control a page or not. It returns a
Promisethat will never reject, which resolves to aServiceWorkerRegistrationwith anServiceWorkerRegistration.activeworker.
Event handlers
ServiceWorkerContainer.oncontrollerchange- An event handler fired whenever a
controllerchangeevent occurs — when the document's associatedServiceWorkerRegistrationacquires a newServiceWorkerRegistration.activeworker. ServiceWorkerContainer.onerror- An event handler fired whenever an
errorevent occurs in the associated service workers. ServiceWorkerContainer.onmessage- An event handler fired whenever a
messageevent occurs — when incoming messages are received to theServiceWorkerContainerobject (e.g. via aMessagePort.postMessage()call.)
Methods
ServiceWorkerContainer.register()- Creates or updates a
ServiceWorkerRegistrationfor the givenscriptURL. ServiceWorkerContainer.getRegistration()- Gets a
ServiceWorkerRegistrationobject whose scope URL matches the provided document URL. If the method can't return aServiceWorkerRegistration, it returns aPromise. ServiceWorkerContainer.getRegistrations()- Returns all
ServiceWorkerRegistrations associated with aServiceWorkerContainerin an array. If the method can't returnServiceWorkerRegistrations, it returns aPromise.
Examples
This code snippet is from the service worker fallback-response sample (see fallback-response live). The code checks to see if the browser supports service workers. Then 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) {
navigator.serviceWorker.register('service-worker.js', {scope: './'}).then(function() {
if (navigator.serviceWorker.controller) {
document.querySelector('#status').textContent = 'The service worker is currently handling network operations.';
showRequestButtons();
} else {
document.querySelector('#status').textContent = 'Please reload this page to allow the service worker to handle network operations.';
}
}).catch(function(error) {
document.querySelector('#status').textContent = error;
});
} else {
var aElement = document.createElement('a');
aElement.href = 'http://www.chromium.org/blink/serviceworker/service-worker-faq';
aElement.textContent = 'unavailable';
document.querySelector('#status').appendChild(aElement);
}
Specifications
| Specification | Status | Comment |
|---|---|---|
| Service Workers The definition of 'ServiceWorkerContainer' in that specification. |
Working Draft | Initial definition. |
Browser compatibility
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
|---|---|---|---|---|---|
| Basic support | 40.0 | 44.0 (44.0)[1] | No support | 24 | No support |
| Feature | Android | Firefox Mobile (Gecko) | Firefox OS | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
|---|---|---|---|---|---|---|---|
| Basic support | ? | 44.0 (44.0) | (Yes) | No support | ? | No support | ? |
[1] Service workers (and Push) have been disabled in the Firefox 45 and 52 Extended Support Releases (ESR.)