ServiceWorkerContainer

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.controller 只读
返回 ServiceWorker object if its state is activated (the same object returned by ServiceWorkerRegistration.active ). This property returns null during a force-refresh request ( Shift + refresh) or if there is no active worker.
ServiceWorkerContainer.ready 只读
Provides a way of delaying code execution until a service worker is active. It returns a Promise that will never reject, and which waits indefinitely until the ServiceWorkerRegistration associated with the current page has an ServiceWorkerRegistration.active worker. Once that condition is met, it resolves with the ServiceWorkerRegistration .

Events

controllerchange
Occurs when the document's associated ServiceWorkerRegistration acquires a new active worker.
Also available via the ServiceWorkerContainer.oncontrollerchange 特性。
error
Fired whenever an error occurs in the associated service workers.
Also available via the ServiceWorkerContainer.onerror 特性。
message
Occurs when incoming messages are received by the ServiceWorkerContainer object (e.g. via a MessagePort.postMessage() call.)
Also available via the ServiceWorkerContainer.onmessage 特性。

方法

ServiceWorkerContainer.register()
Creates or updates a ServiceWorkerRegistration for the given scriptURL .
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.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.

范例

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(function(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(function(error) {
    console.log('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 = function() {
    console.log('This page is now controlled by:', navigator.serviceWorker.controller);
  };
} else {
  console.log('Service workers are not supported.');
}
					

规范

规范 状态 Comment
服务工作者
The definition of 'ServiceWorkerContainer' in that specification.
工作草案 初始定义。

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
ServiceWorkerContainer
Chrome 40 Edge 17
17
16
Disabled
Disabled From version 16: this feature is behind the Enable service workers preference.
Firefox 44
注意事项
44
注意事项
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
IE 不支持 No Opera 27 Safari 11.1 WebView Android 40 Chrome Android 40 Firefox Android 44 Opera Android 27 Safari iOS 11.3 Samsung Internet Android 4.0
controller
Chrome 40 Edge 17
17
16
Disabled
Disabled From version 16: this feature is behind the Enable service workers preference.
Firefox 44
注意事项
44
注意事项
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
IE 不支持 No Opera 27 Safari 11.1 WebView Android 40 Chrome Android 40 Firefox Android 44 Opera Android 27 Safari iOS 11.3 Samsung Internet Android 4.0
getRegistration
Chrome 40 Edge 17
17
16
Disabled
Disabled From version 16: this feature is behind the Enable service workers preference.
Firefox 44
注意事项
44
注意事项
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
IE 不支持 No Opera 27 Safari 11.1 WebView Android 40 Chrome Android 40 Firefox Android 44 Opera Android 27 Safari iOS 11.3 Samsung Internet Android 4.0
getRegistrations
Chrome 45 Edge 17
17
16
Disabled
Disabled From version 16: this feature is behind the Enable service workers preference.
Firefox 44
注意事项
44
注意事项
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
IE 不支持 No Opera 27 Safari 11.1 WebView Android 40 Chrome Android 45 Firefox Android 44 Opera Android 27 Safari iOS 11.3 Samsung Internet Android 4.0
message event Chrome 40 Edge 17
17
16
Disabled
Disabled From version 16: this feature is behind the Enable service workers preference.
Firefox 44
注意事项
44
注意事项
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
IE 不支持 No Opera 27 Safari 11.1 WebView Android 40 Chrome Android 40 Firefox Android 44 Opera Android 27 Safari iOS 11.3 Samsung Internet Android 4.0
oncontrollerchange
Chrome 40 Edge 17
17
16
Disabled
Disabled From version 16: this feature is behind the Enable service workers preference.
Firefox 44
注意事项
44
注意事项
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
IE 不支持 No Opera 27 Safari 11.1 WebView Android 40 Chrome Android 40 Firefox Android 44 Opera Android 27 Safari iOS 11.3 Samsung Internet Android 4.0
onerror
Chrome 40 Edge 17
17
16
Disabled
Disabled From version 16: this feature is behind the Enable service workers preference.
Firefox 44
注意事项
44
注意事项
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
IE 不支持 No Opera 27 Safari 11.1 WebView Android 40 Chrome Android 40 Firefox Android 44 Opera Android 27 Safari iOS 11.3 Samsung Internet Android 4.0
onmessage
Chrome 40 Edge 17
17
16
Disabled
Disabled From version 16: this feature is behind the Enable service workers preference.
Firefox 44
注意事项
44
注意事项
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
IE 不支持 No Opera 27 Safari 11.1 WebView Android 40 Chrome Android 40 Firefox Android 44 Opera Android 27 Safari iOS 11.3 Samsung Internet Android 4.0
onmessageerror Chrome 不支持 No Edge 不支持 17 — 79 Firefox Yes IE 不支持 No Opera ? Safari Yes WebView Android 不支持 No Chrome Android 不支持 No Firefox Android Yes Opera Android ? Safari iOS Yes Samsung Internet Android 不支持 No
ready
Chrome 40 Edge 17
17
16
Disabled
Disabled From version 16: this feature is behind the Enable service workers preference.
Firefox 44
注意事项
44
注意事项
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
IE 不支持 No Opera 27 Safari 11.1 WebView Android 40 Chrome Android 40 Firefox Android 44 Opera Android 27 Safari iOS 11.3 Samsung Internet Android 4.0
register
Chrome 40 Edge 17
17
16
Disabled
Disabled From version 16: this feature is behind the Enable service workers preference.
Firefox 44
注意事项
44
注意事项
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
IE 不支持 No Opera 27 Safari 11.1 WebView Android 40 Chrome Android 40 Firefox Android 44 Opera Android 27 Safari iOS 11.3 Samsung Internet Android 4.0
startMessages Chrome 74 Edge ≤79 Firefox 64 IE 不支持 No Opera 62 Safari Yes WebView Android 74 Chrome Android 74 Firefox Android 64 Opera Android 50 Safari iOS Yes Samsung Internet Android 11.0

图例

完整支持
完整支持
不支持
不支持
兼容性未知
兼容性未知
实验。期望将来行为有所改变。
实验。期望将来行为有所改变。
见实现注意事项。
用户必须明确启用此特征。
用户必须明确启用此特征。

另请参阅