GO-Ve01
ServicesDevelopersBooksDictionaryPapersWikidataEncyclopediaSearchBabelCareersDeck

ServiceWorker

Service Workers API

Service Workers API

The **`ServiceWorker`** interface of the [Service Worker API](/docs/Web/API/Service_Worker_API) provides a reference to a service worker. Multiple browsing contexts (e.g., pages, workers, etc.) can be associated with the same service worker, each through a unique `ServiceWorker` object.

A `ServiceWorker` object is available via a number of properties:

- ServiceWorkerRegistration.active
- ServiceWorkerGlobalScope.serviceWorker
- ServiceWorkerContainer.controller — when the service worker is in `activating` or `activated` state
- ServiceWorkerRegistration.installing — when the service worker is in `installing` state
- ServiceWorkerRegistration.waiting — when the service worker is in `installed` state

The ServiceWorker.state property and [`statechange` event](/docs/Web/API/ServiceWorker/statechange_event) can be used to check and observe changes in the lifecycle-state of the object's associated service worker.
Related lifecycle events, such as [`install`](/docs/Web/API/ServiceWorkerGlobalScope/install_event) and [`activate`](/docs/Web/API/ServiceWorkerGlobalScope/activate_event) are fired at the service worker itself.

Service workers allow static import of [ECMAScript modules](/docs/Web/JavaScript/Guide/Modules), if supported, using [`import`](/docs/Web/JavaScript/Reference/Statements/import).
Dynamic import is disallowed by the specification — calling [`import()`](/docs/Web/JavaScript/Reference/Operators/import) will throw.

Service workers can only be registered in the Window scope in some or all browsers, because the `ServiceWorker` object is not exposed to DedicatedWorkerGlobalScope and SharedWorkerGlobalScope.
Check the [browser compatibility](#browser_compatibility) for information.

## Instance properties

_The `ServiceWorker` interface inherits properties from its parent, EventTarget._

- ServiceWorker.scriptURL
- : Returns the `ServiceWorker` serialized script URL defined as part of ServiceWorkerRegistration. The URL must be on the same origin as the document that registers the `ServiceWorker`.
- ServiceWorker.state
- : Returns the state of the service worker. It returns one of the following values: `parsed`, `installing`, `installed`, `activating`, `activated`, or `redundant`.

## Instance methods

_The `ServiceWorker` interface inherits methods from its parent, EventTarget._

- ServiceWorker.postMessage()
- : Sends a message — consisting of any [structured-cloneable](/docs/Web/API/Web_Workers_API/Structured_clone_algorithm) JavaScript object — to the service worker. The message is transmitted to the service worker using a message event on its global scope.

## Events

- statechange
- : Fired when ServiceWorker.state changes.

- error
- : Fired when an error happens inside the `ServiceWorker` object.

## Examples

This code snippet is from the [service worker registration-events sample](https://github.com/GoogleChrome/samples/blob/gh-pages/service-worker/registration-events/index.html) ([live demo](https://googlechrome.github.io/samples/service-worker/registration-events/)). The code listens for any change in the ServiceWorker.state and returns its value.

```js
if ("serviceWorker" in navigator) {
navigator.serviceWorker
.register("service-worker.js", {
scope: "./",
})
.then((registration) => {
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);
});
}
})
.catch((error) => {
// Something went wrong during registration. The service-worker.js file
// might be unavailable or contain a syntax error.
});
} else {
// The current browser doesn't support service workers.
// Perhaps it is too old or we are not in a Secure Context.
}
```

## Specifications

## Browser compatibility

## See also

- [The Offline Cookbook](https://web.dev/articles/offline-cookbook) (service workers)
- [Using Service Workers](/docs/Web/API/Service_Worker_API/Using_Service_Workers)
- [Service worker basic code example](https://github.com/mdn/dom-examples/tree/main/service-worker/simple-service-worker)
- [Using web workers](/docs/Web/API/Web_Workers_API/Using_web_workers)

source: MDN Web Docs mirror · page

Cite this

"ServiceWorker". MDN Web Docs. CC BY-SA 2.5 (MDN). https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker

JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy

Save to pass ·