--- title: ServiceWorkerContainer slug: Web/API/ServiceWorkerContainer tags: - API - Draft - Interface - NeedsTranslation - Offline - Reference - Service Workers - ServiceWorkerContainer - TopicStub - Workers translation_of: Web/API/ServiceWorkerContainer ---
{{SeeCompatTable}}{{APIRef("Service Workers API")}}
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 {{domxref("ServiceWorkerContainer.register", "ServiceWorkerContainer.register(scriptURL, scope[, base])")}} method used to register service workers, and the {{domxref("ServiceWorkerContainer.controller")}} property used to determine whether or not the current page is actively controlled.
activated
(the same object returned by {{domxref("ServiceWorkerRegistration.active")}}). This property returns null
if the request is a force refresh (Shift + refresh) or if there is no active worker.scriptURL
.Promise
. ServiceWorkerContainer
in an array. If the method can't return {{domxref("ServiceWorkerRegistration")}}s, it returns a Promise
. 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); }
Specification | Status | Comment |
---|---|---|
{{SpecName('Service Workers', '#service-worker-container', 'ServiceWorkerContainer')}} | {{Spec2('Service Workers')}} | Initial definition. |
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Basic support | {{CompatChrome(40.0)}} | {{ CompatGeckoDesktop("44.0") }} | {{CompatNo}} | 24 | {{CompatNo}} |
Feature | Android | Firefox Mobile (Gecko) | Firefox OS | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|---|
Basic support | {{CompatUnknown}} | {{ CompatGeckoMobile("44.0") }} | {{ CompatVersionUnknown }} | {{CompatNo}} | {{CompatUnknown}} | {{CompatNo}} | {{CompatUnknown}} |