Skip to main content

fetch event

BFChainPlugindwebServiceWorkerfetch eventLess than 1 minute

The fetch event is used to receive messages sent from other apps. To ensure the app can receive messages, register this event when the app is initialized.

In the added listener, return data to the sender via respondWith.

Example

import { dwebServiceWorker } from "@plaoc/plugins";

dwebServiceWorker.addEventListener(
  "fetch",
  async (event: ServiceWorkerFetchEvent) => {
    const data = await event.getRemoteManifest();
    console.log("Dweb Service Worker fetch!", data);

    const url = new URL(event.request.url);
    if (url.pathname.endsWith("/say/hi")) {
      const hiMessage = url.searchParams.get("message");
      console.log(`Received: ${hiMessage}`);

      // Send message back
      return event.respondWith(`I am plaoc-html-demo I received the message`);
    }

    return event.respondWith("Not match any routes");
  }
);

Event arguments

The type of event received by ServiceWorkerFetchEvent provides the functionality to query the connector's information and return content.

getRemoteManifest

Get connector information.

Function signature

getRemoteManifest(): Promise<$JmmAppInstallManifest>

Return value

respondWith

This function is responsible for returning messages to the sender.

Function signature

respondWith(body?: BodyInit | null, init?: ResponseInit): Promise<void>

Parameters

type BodyInit = ReadableStream | XMLHttpRequestBodyInit;

type XMLHttpRequestBodyInit =
  | Blob
  | BufferSource
  | FormData
  | URLSearchParams
  | string;

Platform compatibility

Attribute/Method/EventAndroidIOSDesktop
event fetch

DwebServiceWorker

Comments
  • Latest
  • Oldest
  • Hottest
Powered by Waline v2.15.7