FetchEvent: handled プロパティ

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since April 2018.

メモ: この機能はサービスワーカー内でのみ利用可能です。

handledFetchEvent インターフェイスのプロパティで、このイベントがフェッチアルゴリズムによって処理されたかどうかを示すプロミスを返します。このプロパティは、ブラウザーがレスポンスを消費した後にコードを実行することができ、通常は waitUntil() メソッドと共に使用します。

イベントが処理されるまでは待機し、イベントが処理されると履行される Promise です。

js
addEventListener("fetch", (event) => {
  event.respondWith(
    (async function () {
      const response = await doCalculateAResponse(event.request);

      event.waitUntil(
        (async function () {
          await doSomeAsyncStuff(); // オプション

          // イベントがブラウザーによって消費されるのを待つ
          await event.handled;

          return doFinalStuff(); // イベントが消費された後に後始末
        })(),
      );

      return response;
    })(),
  );
});

仕様書

Specification
Service Workers
# dom-fetchevent-handled

ブラウザーの互換性

関連情報