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)

WARNING
The scriptURL parameter passed to this method represents the URL of an external script loaded into a service worker.
APIs like this are known as injection sinks, and are potentially a vector for cross-site scripting (XSS) attacks.
[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

You can mitigate this risk by having a Content Security Policy (CSP) that restricts the locations from which scripts can be loaded, and by always assigning TrustedScriptURL objects instead of strings and enforcing trusted types.
See Security considerations for more information.

The register() method of the ServiceWorkerContainer interface creates or updates a ServiceWorkerRegistration for the given scope.

-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