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

廉价感情. 提交于 2019-12-11 12:52:10

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/52721826/how-do-i-access-the-local-storage-using-service-workers-in-angular

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!