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
只读
ServiceWorker
.
ServiceWorkerRegistration.installing
只读
installing
. This is initially set to
null
.
ServiceWorkerRegistration.waiting
只读
installed
. This is initially set to
null
.
ServiceWorkerRegistration.active
只读
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
只读
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.
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
只读
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()
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
特性。
|
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
ServiceWorkerRegistration
|
Chrome 40 |
Edge
17
|
Firefox
44
|
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
|
Firefox
44
|
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
|
Firefox
46
|
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
|
Firefox
44
|
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
|
Firefox
44
|
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
|
Firefox
44
|
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
|
Edge
≤79
Disabled
|
Firefox ? | IE No | Opera Yes | Safari ? | WebView Android Yes |
Chrome Android
56
Disabled
|
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
|
Firefox
44
|
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
|
Firefox
44
|
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
|
Firefox
46
|
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
|
Firefox
44
|
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
|
Edge
17
|
Firefox
44
|
IE No |
Opera
32
|
Safari 11.1 |
WebView Android
45
|
Chrome Android
45
|
Firefox Android 44 |
Opera Android
32
|
Safari iOS No |
Samsung Internet Android
4.0
|
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
|
Firefox
44
|
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 |
完整支持
不支持
兼容性未知
实验。期望将来行为有所改变。
非标。预期跨浏览器支持较差。
见实现注意事项。
用户必须明确启用此特征。
ServiceWorkerRegistration