Table of Contents

Class ServiceWorkerContainer

Namespace
CSharpToJavaScript.APIs.JS
Assembly
CSharpToJavaScript.dll

The ServiceWorkerContainer interface of the Service Worker API provides an object representing the service worker as an overall unit in the network ecosystem, including facilities to register, unregister and update service workers, and access the state of service workers and their registrations.

[Value("ServiceWorkerContainer")]
public class ServiceWorkerContainer : EventTarget
Inheritance
ServiceWorkerContainer
Inherited Members

Remarks

Most importantly, it exposes the Register(Union137, RegistrationOptions) method used to register service workers, and the Controller property used to determine whether or not the current page is actively controlled.

ServiceWorkerContainer objects are exposed in the Window scope through ServiceWorker and in workers using ServiceWorker (if supported — check browser compatibility).

-Using Service Workers
-Service workers basic code example
-Using web workers

See also on MDN

Constructors

ServiceWorkerContainer()

public ServiceWorkerContainer()

Properties

Controller

The controller read-only property of the ServiceWorkerContainer interface represents the active {{domxref("ServiceWorker","service worker","","nocode")}} controlling the current page (associated with this ServiceWorkerContainer), or null if the page has no active or activating service worker.

[Value("controller")]
public ServiceWorker? Controller { get; }

Property Value

ServiceWorker

A ServiceWorker object if its state is activating or activated, or null if there is no active worker.The property also returns null if the request is a force refresh (<kbd>Shift</kbd> + refresh)

Remarks

This is the same object returned by Active.

See also on MDN

Oncontrollerchange

[Value("oncontrollerchange")]
public EventHandlerNonNull Oncontrollerchange { get; set; }

Property Value

EventHandlerNonNull

Onmessage

[Value("onmessage")]
public EventHandlerNonNull Onmessage { get; set; }

Property Value

EventHandlerNonNull

Onmessageerror

[Value("onmessageerror")]
public EventHandlerNonNull Onmessageerror { get; set; }

Property Value

EventHandlerNonNull

Ready

The ready read-only property of the ServiceWorkerContainer interface provides a way of delaying code execution until a service worker is active.

[Value("ready")]
public Task<ServiceWorkerRegistration> Ready { get; }

Property Value

Task<ServiceWorkerRegistration>

A {{jsxref("Promise")}} that will never reject, and which may eventually resolve with a ServiceWorkerRegistration when there is an active service worker.

Remarks

The property returns a {{jsxref("Promise")}} that will never reject, and which waits indefinitely until the ServiceWorkerRegistration associated with the current page has an Active worker.
Once that condition is met, it resolves with the ServiceWorkerRegistration.

See also on MDN

Methods

GetRegistration(string)

The getRegistration() method of the
ServiceWorkerContainer interface gets a
ServiceWorkerRegistration object whose scope URL matches the provided
client URL. The method returns a Promise that resolves to
a ServiceWorkerRegistration or undefined.

[Value("getRegistration")]
public Task<ServiceWorkerRegistration> GetRegistration(string clientURL = null)

Parameters

clientURL string

Returns

Task<ServiceWorkerRegistration>

A {{jsxref("Promise")}} that resolves to a ServiceWorkerRegistration
object or undefined.

Remarks

GetRegistrations()

The getRegistrations() method of the
ServiceWorkerContainer interface gets all
ServiceWorkerRegistrations associated with a
ServiceWorkerContainer, in an array. The method returns a
Promise that resolves to an array of
ServiceWorkerRegistration.

[Value("getRegistrations")]
public Task<ServiceWorkerRegistration[]> GetRegistrations()

Returns

Task<ServiceWorkerRegistration[]>

A Promise that resolves to an array of
ServiceWorkerRegistration objects.

Remarks

Register(Union137, RegistrationOptions)

The register() method of the ServiceWorkerContainer interface creates or updates a ServiceWorkerRegistration for the given scope.
If successful, the registration associates the provided script URL to a scope, which is subsequently used for matching documents to a specific service worker.

[Value("register")]
public Task<ServiceWorkerRegistration> Register(Union137 scriptURL, RegistrationOptions options = null)

Parameters

scriptURL Union137
options RegistrationOptions

Returns

Task<ServiceWorkerRegistration>

A {{jsxref("Promise")}} that resolves with a ServiceWorkerRegistration object.

Remarks

A single registration is created for each unique scope.
If register() is called for a scope that has an existing registration, the registration is updated with any changes to the scriptURL or options.
If there are no changes, then the existing registration is returned.
Note that calling register() with the same scope and scriptURL does not restart the installation process.
You can therefore call this method unconditionally from a controlled page: you don&apos;t need to first check whether there&apos;s an active registration or service worker.

A document can potentially be within the scope of several registrations with different service workers and options.
The browser will associate the document with the matching registration that has the most specific scope.
This ensures that only one service worker runs for each document.

NOTE

It is generally safer not to define registrations that have overlapping scopes.

-ServiceWorkerRegistration: unregister() method
-Service worker API
-Using service workers
-Service-Worker-Allowed HTTP header

See also on MDN

StartMessages()

The startMessages() method of
the ServiceWorkerContainer interface explicitly starts the flow of
messages being dispatched from a service worker to pages under its control (e.g., sent
via PostMessage(dynamic, List<Object>)). This can be used to react to sent messages
earlier, even before that page&apos;s content has finished loading.

[Value("startMessages")]
public GlobalObject.Undefined StartMessages()

Returns

GlobalObject.Undefined

undefined.

Remarks