Chartbreaker
    Preparing search index...

    Interface ServiceWorkerContainer

    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. Available only in secure contexts.

    MDN Reference

    interface ServiceWorkerContainer {
        controller: ServiceWorker | null;
        oncontrollerchange:
            | ((this: ServiceWorkerContainer, ev: Event) => any)
            | null;
        onmessage: ((this: ServiceWorkerContainer, ev: MessageEvent) => any) | null;
        onmessageerror:
            | ((this: ServiceWorkerContainer, ev: MessageEvent) => any)
            | null;
        ready: Promise<ServiceWorkerRegistration>;
        addEventListener<K extends keyof ServiceWorkerContainerEventMap>(
            type: K,
            listener: (
                this: ServiceWorkerContainer,
                ev: ServiceWorkerContainerEventMap[K],
            ) => any,
            options?: boolean | AddEventListenerOptions,
        ): void;
        addEventListener(
            type: string,
            listener: EventListenerOrEventListenerObject,
            options?: boolean | AddEventListenerOptions,
        ): void;
        dispatchEvent(event: Event): boolean;
        getRegistration(
            clientURL?: string | URL,
        ): Promise<ServiceWorkerRegistration | undefined>;
        getRegistrations(): Promise<readonly ServiceWorkerRegistration[]>;
        register(
            scriptURL: string | URL,
            options?: RegistrationOptions,
        ): Promise<ServiceWorkerRegistration>;
        removeEventListener<K extends keyof ServiceWorkerContainerEventMap>(
            type: K,
            listener: (
                this: ServiceWorkerContainer,
                ev: ServiceWorkerContainerEventMap[K],
            ) => any,
            options?: boolean | EventListenerOptions,
        ): void;
        removeEventListener(
            type: string,
            listener: EventListenerOrEventListenerObject,
            options?: boolean | EventListenerOptions,
        ): void;
        startMessages(): void;
    }

    Hierarchy (View Summary)

    Index

    Properties

    controller: ServiceWorker | null

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

    MDN Reference

    oncontrollerchange: ((this: ServiceWorkerContainer, ev: Event) => any) | null
    onmessage: ((this: ServiceWorkerContainer, ev: MessageEvent) => any) | null
    onmessageerror: ((this: ServiceWorkerContainer, ev: MessageEvent) => any) | null

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

    MDN Reference

    Methods

    • 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

      Returns boolean

    • 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.

      MDN Reference

      Parameters

      • OptionalclientURL: string | URL

      Returns Promise<ServiceWorkerRegistration | undefined>

    • 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.

      MDN Reference

      Returns Promise<readonly ServiceWorkerRegistration[]>

    • 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 Client.postMessage()). This can be used to react to sent messages earlier, even before that page's content has finished loading.

      MDN Reference

      Returns void