Skip to main content

messageSW

First added

Workbox

About

Sends a data object to a service worker via postMessage and resolves with a response (if any).

A response can be sent by calling event.ports[0].postMessage(...) in the service worker, which will resolve the promise returned by messageSW(). If no response is sent, the promise will never resolve.

Parameters

  • sw — The service worker to send the message to.
  • data — An object to send to the service worker.

Usage

declare const sw: ServiceWorker;
// ---cut-before---
import { messageSW } from "@serwist/window";

const swVersion = await messageSW(sw, { type: "GET_VERSION" });

console.log("Service worker version:", swVersion);