← Back to release summary

ServiceWorkerGlobalScope#serviceWorker to get a service worker object corresponding to a worker itself

Category
Service Worker
Type
New or changed feature
Status
Enabled by default (Chrome 79)
Intent stage
Prepare to ship

Summary

This allows a service worker to get its ServiceWorker object with self.serviceWorker in a service worker script and its current state with self.serviceWorker.state in the script.

Motivation

A service worker instance currently has no way to get its current lifecycle state. There's a bit of a hack one can do where you keep track of the current event as a global variable but that's error prone and doesn't capture the waiting periods correctly. The best way to solve this would be to just allow a service worker to get its ServiceWorker object in a service worker script.

Standards & signals

Docs: https://github.com/w3c/ServiceWorker/issues/1077

Samples: https://github.com/web-platform-tests/wpt/blob/master/service-workers/service-worker/global-serviceworker.https.any.js

Explainers: https://github.com/w3c/ServiceWorker/issues/1077

View on chromestatus.com