【ServiceWorker.state】
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.
ServiceWorkerRegistration.installing
The installing property of the ServiceWorkerRegistration interface returns a service worker whoseServiceWorker.state is installing. This property is initially set to null.
ServiceWorkerRegistration.waiting
The waiting property of the ServiceWorkerRegistration interface returns a service worker whoseServiceWorker.state is installed. This property is initially set to null.
ServiceWorkerRegistration.active
The active property of the ServiceWorkerRegistration interface returns a service worker whoseServiceWorker.state is activating or activated. This property is initially set to null.
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); }); }
ServiceWorkerContainer.controller
The controller read-only property of the ServiceWorkerContainer interface returns a ServiceWorker object if its state is activated (the same object returned by ServiceWorkerRegistration.active). This property returns null if the request is a force refresh (Shift + refresh) or if there is no active worker.
参考:
1、https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/state
2、https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/waiting