Skip to main content

ServiceWorker

The ServiceWorker interface of the 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. Available only in secure contexts.

MDN Reference

Extends​

Methods​

addEventListener()​

Call Signature​

addEventListener<K>(
type,
listener,
options?
): void;

The addEventListener() method of the EventTarget interface sets up a function that will be called whenever the specified event is delivered to the target.

MDN Reference

Type Parameters​
Type Parameter
K extends keyof ServiceWorkerEventMap
Parameters​
ParameterType
typeK
listener(this, ev) => any
options?boolean | AddEventListenerOptions
Returns​

void

Overrides​

EventTarget.addEventListener

Call Signature​

addEventListener(
type,
listener,
options?
): void;

The addEventListener() method of the EventTarget interface sets up a function that will be called whenever the specified event is delivered to the target.

MDN Reference

Parameters​
ParameterType
typestring
listenerEventListenerOrEventListenerObject
options?boolean | AddEventListenerOptions
Returns​

void

Overrides​

AbstractWorker.addEventListener


dispatchEvent()​

dispatchEvent(event): boolean;

The dispatchEvent() method of the EventTarget sends an Event to the object, (synchronously) invoking the affected event listeners in the appropriate order. The normal event processing rules (including the capturing and optional bubbling phase) also apply to events dispatched manually with dispatchEvent().

MDN Reference

Parameters​

ParameterType
eventEvent

Returns​

boolean

Inherited from​

EventTarget.dispatchEvent


postMessage()​

Call Signature​

postMessage(message, transfer): void;

The postMessage() method of the ServiceWorker interface sends a message to the worker. The first parameter is the data to send to the worker. The data may be any JavaScript object which can be handled by the structured clone algorithm.

MDN Reference

Parameters​
ParameterType
messageany
transferTransferable[]
Returns​

void

Call Signature​

postMessage(message, options?): void;
Parameters​
ParameterType
messageany
options?StructuredSerializeOptions
Returns​

void


removeEventListener()​

Call Signature​

removeEventListener<K>(
type,
listener,
options?
): void;

The removeEventListener() method of the EventTarget interface removes an event listener previously registered with EventTarget.addEventListener() from the target. The event listener to be removed is identified using a combination of the event type, the event listener function itself, and various optional options that may affect the matching process; see Matching event listeners for removal.

MDN Reference

Type Parameters​
Type Parameter
K extends keyof ServiceWorkerEventMap
Parameters​
ParameterType
typeK
listener(this, ev) => any
options?boolean | EventListenerOptions
Returns​

void

Overrides​

EventTarget.removeEventListener

Call Signature​

removeEventListener(
type,
listener,
options?
): void;

The removeEventListener() method of the EventTarget interface removes an event listener previously registered with EventTarget.addEventListener() from the target. The event listener to be removed is identified using a combination of the event type, the event listener function itself, and various optional options that may affect the matching process; see Matching event listeners for removal.

MDN Reference

Parameters​
ParameterType
typestring
listenerEventListenerOrEventListenerObject
options?boolean | EventListenerOptions
Returns​

void

Overrides​

AbstractWorker.removeEventListener

Properties​

onerror​

onerror: ((this, ev) => any) | null;

MDN Reference

Inherited from​

AbstractWorker.onerror


onstatechange​

onstatechange: ((this, ev) => any) | null;

MDN Reference


scriptURL​

readonly scriptURL: string;

Returns the ServiceWorker serialized script URL defined as part of ServiceWorkerRegistration. Must be on the same origin as the document that registers the ServiceWorker.

MDN Reference


state​

readonly state: ServiceWorkerState;

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: parsed, installing, installed, activating, activated, or redundant.

MDN Reference