MDN Web Docs

Value

A String that can take one of the following values:

"parsed"

The initial state of a service worker after it is downloaded and confirmed to be runnable. A service worker is never updated to this state, so this will never be the value of the statechange event.

"installing"

The service worker in this state is considered an installing worker. During this state, ExtendableEvent.waitUntil() can be called inside the install event handler to extend the life of the installing worker until the passed promise resolves successfully. This is primarily used to ensure that the service worker is not active until all of the core caches are populated.

"installed"

The service worker in this state is considered a waiting worker.

"activating"

The service worker in this state is considered an active worker. During this state, ExtendableEvent.waitUntil() can be called inside the onactivate event handler to extend the life of the active worker until the passed promise resolves successfully. No functional events are dispatched until the state becomes activated.

"activated"

The service worker in this state is considered an active worker ready to handle functional events.

"redundant"

A new service worker is replacing the current service worker, or the current service worker is being discarded due to an install failure.

Examples

This code snippet is from the service worker registration-events sample (live demo). The code listens for any change in the ServiceWorker.state and returns its value.

js

let serviceWorker;
if (registration.installing) {
  serviceWorker = registration.installing;
  document.querySelector("#kind").textContent = "installing";
} else if (registration.waiting) {
  serviceWorker = registration.waiting;
  document.querySelector("#kind").textContent = "waiting";
} else if (registration.active) {
  serviceWorker = registration.active;
  document.querySelector("#kind").textContent = "active";
}
if (serviceWorker) {
  logState(serviceWorker.state);
  serviceWorker.addEventListener("statechange", (e) => {
    logState(e.target.state);
  });
}

Specifications

Specification
Service Workers Nightly
# service-worker-state

Browser compatibility

Help improve MDN

Yes No

Learn how to contribute

This page was last modified on by MDN contributors.

Read the original on developer.mozilla.org ↗