Service Workers API
The ServiceWorkerRegistration interface of the Service Worker 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 objects.
Instance properties
Also inherits properties from its parent interface, EventTarget.
- ServiceWorkerRegistration.active
- : Returns a service worker whose state is
activatingoractivated. This is initially set tonull. An active worker will control a Client if the client's URL falls within the scope of the registration (thescopeoption set when ServiceWorkerContainer.register is first called.)
- : Returns a service worker whose state is
- ServiceWorkerRegistration.backgroundFetch
- : Returns a reference to a BackgroundFetchManager object, which manages background fetch operations.
- ServiceWorkerRegistration.cookies
- : Returns a reference to the CookieStoreManager interface, which allows subscribe and unsubscribe to cookie change events.
- ServiceWorkerRegistration.index
- : Returns a reference to the ContentIndex interface, for managing indexed content for offline viewing.
- ServiceWorkerRegistration.installing
- : Returns a service worker whose state is
installing. This is initially set tonull.
- : Returns a service worker whose state is
- ServiceWorkerRegistration.navigationPreload
- : Returns the instance of NavigationPreloadManager associated with the current service worker registration.
- ServiceWorkerRegistration.paymentManager
- : Returns a payment app's PaymentManager instance, which is used to manage various payment app functionality.
- ServiceWorkerRegistration.periodicSync
- : Returns a reference to the PeriodicSyncManager interface, which allows for registering of tasks to run at specific intervals.
- ServiceWorkerRegistration.pushManager
- : Returns a reference to the PushManager interface for managing push subscriptions including subscribing, getting an active subscription, and accessing push permission status.
- ServiceWorkerRegistration.scope
- : Returns a string representing a URL that defines a service worker's registration scope; that is, the range of URLs the service worker can control.
- ServiceWorkerRegistration.sync
- : Returns a reference to the SyncManager interface, which manages background synchronization processes.
- ServiceWorkerRegistration.updateViaCache
- : Returns the value of the setting used to determine the circumstances in which the browser will consult the HTTP cache when it tries to update the service worker or any scripts that are imported via WorkerGlobalScope.importScripts. It can be one of the following:
imports,all, ornone.
- : Returns the value of the setting used to determine the circumstances in which the browser will consult the HTTP cache when it tries to update the service worker or any scripts that are imported via WorkerGlobalScope.importScripts. It can be one of the following:
- ServiceWorkerRegistration.waiting
- : Returns a service worker whose state is
installed. This is initially set tonull.
- : Returns a service worker whose state is
Instance methods
Also inherits methods from its parent interface, EventTarget.
- ServiceWorkerRegistration.getNotifications()
- : Returns a list of the notifications in the order that they were created from the current origin via the current service worker registration.
- ServiceWorkerRegistration.showNotification()
- : Displays the notification with the requested title.
- ServiceWorkerRegistration.unregister()
- : Unregisters the service worker registration and returns a Promise. The service worker will finish any ongoing operations before it is unregistered.
- ServiceWorkerRegistration.update()
- : Checks the server for an updated version of the service worker without consulting caches.
Events
- ServiceWorkerRegistration.updatefound_event
- : Fired any time the ServiceWorkerRegistration.installing property acquires a new service worker.
Examples
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((registration) => {
registration.addEventListener("updatefound", () => {
// If updatefound is fired, it means that there's
// a new service worker being installed.
const 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((error) => {
console.error(`Service worker registration failed: ${error}`);
});
} else {
console.error("Service workers are not supported.");
}
Specifications
Browser compatibility
See also
来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。
