ServiceWorkerContainer: ready property
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since April 2018.
Secure context: This feature is available only insecure contexts (HTTPS), in some or allsupporting browsers.
Note: This feature is available inWeb Workers.
Theready read-only property of theServiceWorkerContainer interface provides a way of delaying code execution until a service worker is active.
The property returns aPromise that will never reject, and which waits indefinitely until theServiceWorkerRegistration associated with the current page has anactive worker.Once that condition is met, it resolves with theServiceWorkerRegistration.
In this article
Value
APromise that will never reject, and which may eventually resolve with aServiceWorkerRegistration when there is an active service worker.
Examples
>Deferring code until there is an active service worker
if ("serviceWorker" in navigator) { navigator.serviceWorker.ready.then((registration) => { console.log(`A service worker is active: ${registration.active}`); // At this point, you can call methods that require an active // service worker, like registration.pushManager.subscribe() });} else { console.error("Service workers are not supported.");}Specifications
| Specification |
|---|
| Service Workers Nightly> # navigator-service-worker-ready> |