Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I need to access localstorage or cookie from within the serviceworker

I wish to make a fetch call after the push event for fetching the notif data through an internal api with user specific params which are stored in the localstorage or cookie such as usertype or country id .. how do i do this ?

like image 846
Aman Satija Avatar asked Jan 04 '16 11:01

Aman Satija


People also ask

Can service workers access local storage?

Note: localStorage works in a similar way to service worker cache, but it is synchronous, so not allowed in service workers.

Can service worker access cookies?

In addition, the reliance on document means that cookies cannot be accessed by service workers which cannot access the document object. The Cookie Store API provides an updated method of managing cookies.

Should I use local storage or cookie?

If you need to store data that is accessible for both the server and the client, use cookies. Otherwise, use local storage. If you need to store larger data, use local storage. If you need to store data that does not expire, use local storage.

Can Webworkers access session storage?

No, localStorage and sessionStorage are both undefined in a webworker process. You would have to call postMessage() back to the Worker's originating code, and have that code store the data in localStorage.


1 Answers

You cannot use Local Storage in service workers. It was decided that service workers should not have access to any synchronous APIs. You can use IndexedDB instead, or communicate with the controlled page using postMessage().

By default, cookies are not included with fetch requests, but you can include them as follows: fetch(url, {credentials: 'include'}).

like image 152
Brendan Ritchie Avatar answered Sep 25 '22 07:09

Brendan Ritchie