ServiceWorkerContainer
Experimental
Dies ist eine experimentelle Technologie
Da diese Technologie noch nicht definitiv implementiert wurde, sollte die Browserkompatibilität beachtet werden. Es ist auch möglich, dass die Syntax in einer späteren Spezifikation noch geändert wird.
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
(en-US) property used to determine whether or not the current page is actively controlled.
Properties
ServiceWorkerContainer.controller
(en-US) Schreibgeschützt- Returns a
ServiceWorker
object if its state isactivated
(the same object returned byServiceWorkerRegistration.active
(en-US)). This property returnsnull
if the request is a force refresh (Shift + refresh) or if there is no active worker.
ServiceWorkerContainer.ready
(en-US) Schreibgeschützt- Defines whether a service worker is ready to control a page or not. It returns a
Promise
that will never reject, which resolves to aServiceWorkerRegistration
(en-US) with anServiceWorkerRegistration.active
(en-US) worker.
Event handlers
ServiceWorkerContainer.oncontrollerchange
(en-US)- An event handler fired whenever a
controllerchange
event occurs — when the document's associatedServiceWorkerRegistration
(en-US) acquires a newServiceWorkerRegistration.active
(en-US) worker. ServiceWorkerContainer.onerror
(en-US)- An event handler fired whenever an
error (en-US)
event occurs in the associated service workers. ServiceWorkerContainer.onmessage
(en-US)- An event handler fired whenever a
message
event occurs — when incoming messages are received to theServiceWorkerContainer
object (e.g. via aMessagePort.postMessage()
(en-US) call.)
Methods
ServiceWorkerContainer.register()
- Creates or updates a
ServiceWorkerRegistration
(en-US) for the givenscriptURL
. ServiceWorkerContainer.getRegistration()
(en-US)- Gets a
ServiceWorkerRegistration
(en-US) object whose scope URL matches the provided document URL. If the method can't return aServiceWorkerRegistration
(en-US), it returns aPromise
. ServiceWorkerContainer.getRegistrations()
(en-US)- Returns all
ServiceWorkerRegistration
(en-US)s associated with aServiceWorkerContainer
in an array. If the method can't returnServiceWorkerRegistration
(en-US)s, 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 Die Definition von 'ServiceWorkerContainer' in dieser Spezifikation. |
Arbeitsentwurf | Initial definition. |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Basic support | 40.0 | 44.0 (44.0) | Nicht unterstützt | 24 | Nicht unterstützt |
Feature | Android | Firefox Mobile (Gecko) | Firefox OS | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|---|
Basic support | ? | 44.0 (44.0) | (Ja) | Nicht unterstützt | ? | Nicht unterstützt | ? |