Table of Contents

Class ServiceWorker

Namespace
CSharpToJavaScript.APIs.JS
Assembly
CSharpToJavaScript.dll

The ServiceWorker interface of the Service Worker API provides a reference to a service worker. Multiple browsing context (e.g., pages, workers, etc.) can be associated with the same service worker, each through a unique ServiceWorker object.

[Value("ServiceWorker")]
public class ServiceWorker : EventTarget, AbstractWorker
Inheritance
ServiceWorker
Implements
Inherited Members

Remarks

A ServiceWorker object is available via a number of properties:

The State property and 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 and activate are fired at the service worker itself.

Service workers allow static import of ECMAScript modules, if supported, using import.
Dynamic import is disallowed by the specification — calling 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 for information.

-The Offline Cookbook (service workers)
-Using Service Workers
-Service worker basic code example
-Using web workers

See also on MDN

Constructors

ServiceWorker()

public ServiceWorker()

Properties

Onstatechange

[Value("onstatechange")]
public EventHandlerNonNull Onstatechange { get; set; }

Property Value

EventHandlerNonNull

ScriptURL

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

[Value("scriptURL")]
public string ScriptURL { get; }

Property Value

string

A string.

Remarks

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

[Value("state")]
public ServiceWorkerState State { get; }

Property Value

ServiceWorkerState

A String that can take one of the following values:

Remarks

Methods

PostMessage(dynamic, StructuredSerializeOptions)

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.

[Value("postMessage")]
public GlobalObject.Undefined PostMessage(dynamic message, StructuredSerializeOptions options = null)

Parameters

message dynamic
options StructuredSerializeOptions

Returns

GlobalObject.Undefined

None (GlobalObject.Undefined).

Remarks

The service worker can send back information to its clients by using the PostMessage(dynamic, List<Object>) method. The message will not be sent back to this ServiceWorker object but to the associated ServiceWorkerContainer available via ServiceWorker.

-The ServiceWorker interface it belongs to.
-Its counterpart, the PostMessage(dynamic, List<Object>) method that a service worker must use to send a message back to the associated ServiceWorkerContainer.

See also on MDN

PostMessage(dynamic, List<Object>)

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.

[Value("postMessage")]
public GlobalObject.Undefined PostMessage(dynamic message, List<Object> transfer)

Parameters

message dynamic
transfer List<Object>

Returns

GlobalObject.Undefined

None (GlobalObject.Undefined).

Remarks

The service worker can send back information to its clients by using the PostMessage(dynamic, List<Object>) method. The message will not be sent back to this ServiceWorker object but to the associated ServiceWorkerContainer available via ServiceWorker.

-The ServiceWorker interface it belongs to.
-Its counterpart, the PostMessage(dynamic, List<Object>) method that a service worker must use to send a message back to the associated ServiceWorkerContainer.

See also on MDN