ServiceWorkerContainer Class

The service workers of a window or a worker (https://w3c.github.io/ServiceWorker/#serviceworkercontainer-interface), navigator.serviceWorker: registers the service workers of its origin, gives the service worker that controls the client (ServiceWorkerContainer.Controller), and receives the messages of the service workers.

public sealed class ServiceWorkerContainer : EventTarget

Remarks

The registrations are stored by BrowsingContextOptions.ServiceWorkers. The service workers need a script engine (BrowsingContextOptions.ScriptEngine): they run on their own thread, with their own event loop.

See also

Inheritance
EventTarget ServiceWorkerContainer

Properties

NameDescription
ControllerGets the service worker that controls the client (https://w3c.github.io/ServiceWorker/#dom-serviceworkercontainer-controller), or null.
OnControllerChangeGets or sets the event handler of the controllerchange event.
OnMessageGets or sets the event handler of the message event: setting it enables the client message queue.
OnMessageErrorGets or sets the event handler of the messageerror event.
ReadyGets a promise fulfilled with the registration of the client once it has an active worker (https://w3c.github.io/ServiceWorker/#dom-serviceworkercontainer-ready).

Gets the service worker that controls the client (https://w3c.github.io/ServiceWorker/#dom-serviceworkercontainer-controller), or null.

public ServiceWorker? Controller { get; }

OnControllerChange

ServiceWorkerContainer.cs

Gets or sets the event handler of the controllerchange event.

public Action<Event>? OnControllerChange { get; set; }

Gets or sets the event handler of the message event: setting it enables the client message queue.

public Action<Event>? OnMessage { get; set; }

Gets or sets the event handler of the messageerror event.

public Action<Event>? OnMessageError { get; set; }

Gets a promise fulfilled with the registration of the client once it has an active worker (https://w3c.github.io/ServiceWorker/#dom-serviceworkercontainer-ready).

public Task<ServiceWorkerRegistration> Ready { get; }

Methods

NameDescription
GetRegistration(string)Returns the registration whose scope matches a URL (https://w3c.github.io/ServiceWorker/#dom-serviceworkercontainer-getregistration).
GetRegistrations()Returns the registrations of the origin of the client (https://w3c.github.io/ServiceWorker/#dom-serviceworkercontainer-getregistrations).
Register(TrustedScriptUrl, RegistrationOptions?)Registers a service worker whose script URL was created by a Trusted Types policy (https://w3c.github.io/ServiceWorker/#dom-serviceworkercontainer-register).
Register(string, RegistrationOptions?)Registers a service worker (https://w3c.github.io/ServiceWorker/#dom-serviceworkercontainer-register).
StartMessages()Enables the client message queue: the messages of the service workers are dispatched (https://w3c.github.io/ServiceWorker/#dom-serviceworkercontainer-startmessages).

GetRegistration(string)

ServiceWorkerContainer.cs

Returns the registration whose scope matches a URL (https://w3c.github.io/ServiceWorker/#dom-serviceworkercontainer-getregistration).

public Task<ServiceWorkerRegistration?> GetRegistration(string clientUrl = "")

Parameters

clientUrl string
The URL, relative to the API base URL of the client (the URL of the client by default).

Returns

Task<ServiceWorkerRegistration?>

See also

GetRegistrations()

ServiceWorkerContainer.cs

Returns the registrations of the origin of the client (https://w3c.github.io/ServiceWorker/#dom-serviceworkercontainer-getregistrations).

public Task<IReadOnlyList<ServiceWorkerRegistration>> GetRegistrations()

Register overloads

Register(TrustedScriptUrl, RegistrationOptions?)

ServiceWorkerContainer.cs

Registers a service worker whose script URL was created by a Trusted Types policy (https://w3c.github.io/ServiceWorker/#dom-serviceworkercontainer-register).

public Task<ServiceWorkerRegistration> Register(TrustedScriptUrl scriptUrl, RegistrationOptions? options = null)

Register(string, RegistrationOptions?)

ServiceWorkerContainer.cs

Registers a service worker (https://w3c.github.io/ServiceWorker/#dom-serviceworkercontainer-register).

public Task<ServiceWorkerRegistration> Register(string scriptUrl, RegistrationOptions? options = null)
Parameters
scriptUrl string
The URL of the script, relative to the API base URL of the client.
options RegistrationOptions?
The options: the scope, the type of the script and the update via cache mode.
Returns

Task<ServiceWorkerRegistration>

See also

Enables the client message queue: the messages of the service workers are dispatched (https://w3c.github.io/ServiceWorker/#dom-serviceworkercontainer-startmessages).

public void StartMessages()