How do I call a method on my ServiceWorker from within my page?

后端 未结 2 616
没有蜡笔的小新
没有蜡笔的小新 2021-02-01 23:55

I have a ServiceWorker registered on my page and want to pass some data to it so it can be stored in an IndexedDB and used later for network requests (it\'s an access token).

2条回答
  •  感情败类
    2021-02-02 00:28

    To pass data to your service worker, the above mentioned is a good way. But in case, if someone is still having a hard time implementing that, there is an other hack around for that,

    1 - append your data to get parameter while you load service-worker (for eg., from sw.js -> sw.js?a=x&b=y&c=z)

    2- Now in service worker, fetch those data using self.self.location.search.

    Note, this will be beneficial only if the data you pass do not change for a particular client very often, other wise it will keep changing the loading url of service worker for that particular client and every time the client reloads or revisits, new service worker is installed.

提交回复
热议问题