How do I access the local storage using service workers in angular?
Asked Answered
B

1

1

Okay now i am working on Angular 6 webApp and it is offline first. I am able to setup a fully functional service worker which can retrieve and store requests and response and what not. But, at one point , while registering a sync event, i needed refresh tokens which are stored in the local storage of my webApp. The problem is that service workers are not allowed to access local storage directly, means i don't have any access to the tokens residing in the local storage.How can i make use of local storage in service worker using some api or is there any other strategy to achieve what i want to do (to execute a sync event by replaying the fetch call of failed requests and attaching an updated token with it so that the data gets synced with proper authorization). I even thought of trying it with postmessage() API but it isn't of much use as i have just one worker which is working without any intervention with the angular application.

Billmyre answered 9/10, 2018 at 13:6 Comment(0)
B
3

You can use IndexedDB for storing the token in browser DB. It works similar to local-storage.

https://github.com/mozilla/localForage if you want a simpler interface for IndexedDB.

You can import the localForage lib in a service worker using importScripts, see for example: https://github.com/marco-c/mercurius/blob/master/static/sw-push.js

Bolin answered 9/10, 2018 at 13:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.