ServiceWorker.state
The state
read-only property of the ServiceWorker
interface returns a string representing the current state of the service worker. It can be one of the following values: installing
, installed,
activating
, activated
, or redundant
.
Syntax
someURL = ServiceWorker.state
Value
A String
that can take one of the following values:
"installing"
-
The service worker in this state is considered an installing worker. During this state,
ExtendableEvent.waitUntil()
can be called inside theinstall
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 theonactivate
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.
var 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', function(e) { logState(e.target.state); }); }
Specifications
Specification |
---|
Service Workers 1 # service-worker-state |
Browser compatibility
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
state |
40 |
17 |
44
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
|
No |
27 |
11.1 |
40 |
40 |
44 |
27 |
11.3 |
4.0 |
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/state