--- 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")}}
ServiceWorkerContainer
接口为 service worker提供一个容器般的功能,包括对service worker的注册,卸载 ,更新和访问service worker的状态,以及他们的注册者
主要是{{domxref("ServiceWorkerContainer.register", "ServiceWorkerContainer.register(scriptURL, scope[, base])")}}提供一个注册service worker的方法,{{domxref("ServiceWorkerContainer.controller")}}将获取当前控制页面网络的service worker
scriptURL
?代码是service worker fallback-response sample (see fallback-response live)的其中一段. ?首先检查浏览器是否支持serviceWorker. 代码创建了一个serviceWorker,并且打印出来当前页面的serviceWorker的?是否接管了页面的网络状态. 如果没有需要刷新页面再次查看. 代码也处理了注册失败的情况
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. |
{{CompatibilityTable}}
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Basic support | {{CompatChrome(40.0)}} | {{ CompatGeckoDesktop("44.0") }}[1] | {{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}} |
[1] Service workers (and Push) have been disabled in the Firefox 45 and 52 Extended Support Releases (ESR.)