ServiceWorkerRegistration 接口在 服务工作者 API represents the service worker registration. You register a service worker to control one or more pages that share the same origin.

The lifetime of a service worker registration is beyond that of the ServiceWorkerRegistration objects that represent them within the lifetime of their corresponding service worker clients. The browser maintains a persistent list of active ServiceWorkerRegistration 对象。

注意 : This feature is available in Web 工作者 .

特性

Also implements properties from its parent interface, EventTarget .

ServiceWorkerRegistration.scope 只读
Returns a unique identifier for a service worker registration. This must be on the same origin as the document that registers the ServiceWorker .
ServiceWorkerRegistration.installing 只读
Returns a service worker whose state is installing . This is initially set to null .
ServiceWorkerRegistration.waiting 只读
Returns a service worker whose state is installed . This is initially set to null .
ServiceWorkerRegistration.active 只读
Returns a service worker whose state is activated . This is initially set to null . An active worker will control a ServiceWorkerClient if the client's URL falls within the scope of the registration (the scope option set when ServiceWorkerContainer.register is first called.)
ServiceWorkerRegistration.navigationPreload 只读
Returns the instance of NavigationPreloadManager associated with the current service worker registration.
ServiceWorkerRegistration.pushManager 只读
返回引用针对 PushManager interface for managing push subscriptions including subscribing, getting an active subscription, and accessing push permission status.
ServiceWorkerRegistration.sync 只读
返回引用针对 SyncManager interface, which manages background synchronization processes.
ServiceWorkerRegistration.index 只读
返回引用针对 ContentIndex interface, for managing indexed content for offline viewing.

Unimplemented properties

serviceWorkerRegistration.periodicSync 只读
返回引用针对 PeriodicSyncManager interface, which manages periodic background synchronization processes. This was mentioned as an idea in the SW explainer at some point, but as yet has not been implemented anywhere.

事件处理程序

ServiceWorkerRegistration.onupdatefound 只读
An EventListener property called whenever an event of type updatefound is fired; it is fired any time the ServiceWorkerRegistration.installing property acquires a new service worker.

方法

Also implements methods from its parent interface, EventTarget .

ServiceWorkerRegistration.getNotifications()
返回 Promise that resolves to an array of Notification 对象。
ServiceWorkerRegistration.showNotification()

Displays the notification with the requested title.

ServiceWorkerRegistration.update()

Checks the server for an updated version of the service worker without consulting caches.

ServiceWorkerRegistration.unregister()
Unregisters the service worker registration and returns a Promise . The service worker will finish any ongoing operations before it is unregistered.

范例

In this example, the code first checks whether the browser supports service workers and if so registers one. Next, it adds an updatefound listener in which it uses the service worker registration to listen for further changes to the service worker's state. If the service worker hasn't changed since the last time it was registered, then the updatefound event will not be fired.

if ('serviceWorker' in navigator) {
  navigator.serviceWorker.register('/sw.js')
  .then(function(registration) {
    registration.addEventListener('updatefound', function() {
      // If updatefound is fired, it means that there's
      // a new service worker being installed.
      var installingWorker = registration.installing;
      console.log('A new service worker is being installed:',
        installingWorker);
      // You can listen for changes to the installing service worker's
      // state via installingWorker.onstatechange
    });
  })
  .catch(function(error) {
    console.log('Service worker registration failed:', error);
  });
} else {
  console.log('Service workers are not supported.');
}
						

规范

规范 状态 注释
服务工作者
The definition of 'ServiceWorkerRegistration' in that specification.
工作草案 初始定义。
Push API
The definition of 'PushManager' in that specification.
工作草案 添加 pushManager 特性。
Notifications API 实时标准 添加 showNotification() 方法和 getNotifications() 方法。
Web Background Synchronization 实时标准 添加 sync 特性。

浏览器兼容性

The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
ServiceWorkerRegistration 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
active 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
backgroundFetch Chrome 74 Edge No Firefox No IE No Opera 62 Safari No WebView Android No Chrome Android 74 Firefox Android No Opera Android 53 Safari iOS No Samsung Internet Android 11.0
getNotifications Chrome 40 Edge 17
17
16 Disabled
Disabled From version 16: this feature is behind the Enable service workers preference.
Firefox 46
46
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 No Chrome Android 40 Firefox Android 46 Opera Android 27 Safari iOS 11.3 Samsung Internet Android 4.0
installing 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
navigationPreload Chrome 59 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 46 Safari 11.1 WebView Android 59 Chrome Android 59 Firefox Android 44 Opera Android 43 Safari iOS 11.3 Samsung Internet Android 4.0
onupdatefound 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
paymentManager Chrome 56 Disabled
56 Disabled
Disabled ). To change preferences in Chrome, visit
Edge ≤79 Disabled
≤79 Disabled
Disabled From version ≤79: this feature is behind the #service-worker-payment-apps preference (needs to be set to 启用 ).
Firefox ? IE No Opera Yes Safari ? WebView Android Yes Chrome Android 56 Disabled
56 Disabled
Disabled ). To change preferences in Chrome, visit
Firefox Android ? Opera Android Yes Safari iOS ? Samsung Internet Android No
periodicSync 非标 Chrome 80 Edge 80 Firefox No IE No Opera No Safari No WebView Android 80 Chrome Android 80 Firefox Android No Opera Android No Safari iOS No Samsung Internet Android No
pushManager 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 No Chrome Android 40 Firefox Android 44 Opera Android 27 Safari iOS 11.3 Samsung Internet Android 4.0
scope 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 No Samsung Internet Android 4.0
showNotification Chrome 40 Edge 17
17
16 Disabled
Disabled From version 16: this feature is behind the Enable service workers preference.
Firefox 46
46
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
IE No Opera 27 Safari No WebView Android 40 Chrome Android 40 Firefox Android 46 Opera Android 27 Safari iOS No Samsung Internet Android 4.0
sync Chrome 49 Edge ≤79 Firefox No IE No Opera 36 Safari No WebView Android 49 Chrome Android 49 Firefox Android No Opera Android 36 Safari iOS No Samsung Internet Android 4.0
unregister 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
update Chrome 45
45
Starting with Chrome 46, update() returns a promise that resolves with 'undefined' if the operation completed successfully or there was no update, and rejects if update failed. If the new worker ran but installation failed, the promise still resolves. Formerly, it raised an exception.
Before Chrome 48, this method always bypassed the browser cache. Starting with Chrome 48, it only bypasses the cache when the previous service worker check was more than twenty-four hours ago.
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 32
32
Starting with Opera 33, update() returns a promise that resolves with 'undefined' if the operation completed successfully or there was no update, and rejects if update failed. If the new worker ran but installation failed, the promise still resolves. Formerly, it raised an exception.
Before Opera 35, this method always bypassed the browser cache. Starting with Opera 35, it only bypasses the cache when the previous service worker check was more than twenty-four hours ago.
Safari 11.1 WebView Android 45
45
Starting with Chrome 46, update() returns a promise that resolves with 'undefined' if the operation completed successfully or there was no update, and rejects if update failed. If the new worker ran but installation failed, the promise still resolves. Formerly, it raised an exception.
Before Chrome 48, this method always bypassed the browser cache. Starting with Chrome 48, it only bypasses the cache when the previous service worker check was more than twenty-four hours ago.
Chrome Android 45
45
Starting with Chrome 46, update() returns a promise that resolves with 'undefined' if the operation completed successfully or there was no update, and rejects if update failed. If the new worker ran but installation failed, the promise still resolves. Formerly, it raised an exception.
Before Chrome 48, this method always bypassed the browser cache. Starting with Chrome 48, it only bypasses the cache when the previous service worker check was more than twenty-four hours ago.
Firefox Android 44 Opera Android 32
32
Starting with Opera 33, update() returns a promise that resolves with 'undefined' if the operation completed successfully or there was no update, and rejects if update failed. If the new worker ran but installation failed, the promise still resolves. Formerly, it raised an exception.
Before Opera 35, this method always bypassed the browser cache. Starting with Opera 35, it only bypasses the cache when the previous service worker check was more than twenty-four hours ago.
Safari iOS No Samsung Internet Android 4.0
4.0
Starting with Samsung Internet 5.0, update() returns a promise that resolves with 'undefined' if the operation completed successfully or there was no update, and rejects if update failed. If the new worker ran but installation failed, the promise still resolves. Formerly, it raised an exception.
Before Samsung Internet 5.0, this method always bypassed the browser cache. Starting with Samsung Internet 5.0, it only bypasses the cache when the previous service worker check was more than twenty-four hours ago.
updateViaCache Chrome 68 Edge 18 Firefox 57 IE No Opera Yes Safari 11.1 WebView Android 68 Chrome Android 68 Firefox Android 57 Opera Android Yes Safari iOS Yes Samsung Internet Android 10.0
waiting 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 No Samsung Internet Android 4.0

图例

完整支持

完整支持

不支持

不支持

兼容性未知 ?

兼容性未知

实验。期望将来行为有所改变。

实验。期望将来行为有所改变。

非标。预期跨浏览器支持较差。

非标。预期跨浏览器支持较差。

见实现注意事项。

用户必须明确启用此特征。

用户必须明确启用此特征。

另请参阅

元数据

  • 最后修改: