Client: type プロパティ

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.

typeClient インターフェイスの読み取り専用プロパティで、サービスワーカーが制御しているクライアントの種類を示します。

クライアントの種類を表す文字列。 値は次のいずれかです。

  • "window"
  • "worker"
  • "sharedworker"

js
// サービスワーカークライアント(文書など)
function sendMessage(message) {
  return new Promise((resolve, reject) => {
    // これは ServiceWorker.postMessage バージョンであることに注意してください
    navigator.serviceWorker.controller.postMessage(message);
    window.serviceWorker.onMessage = (e) => {
      resolve(e.data);
    };
  });
}

// 制御するサービスワーカー
self.addEventListener("message", (e) => {
  // e.source はクライアントオブジェクトです
  e.source.postMessage(`こんにちは! あなたのメッセージは: ${e.data}`);
  // type 値も投稿してクライアントに戻しましょう
  e.source.postMessage(e.source.type);
});

仕様書

Specification
Service Workers
# client-type

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
type

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support