0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[ServiceWorker] ブラウザのjsからServiceWorkerに値を渡す

Posted at

前提

  1. hoge.phpにリクエストを投げる。
  2. そのレスポンスにあるfugaというデータをServiceWorkerの中で使いたい。

ソース

レスポンスを受け取ったxxx.jsからmessageイベントを利用すると、たぶん一番簡便。

xxx.js
if ('serviceWorker' in navigator) {
    navigator.serviceWorker.controller.postMessage({type: 'hoge', fuga: xxx});
}

ブラウザ側ではこうやっておいて…

ServiceWorker.js
self.addEventListener('message', event => {
    switch (event.data.type) {
        case 'hoge':
            console.log(event.data.fuga);
            break;
    }
});

こうやる、と (´・ω・`)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?