How do I access the local storage using service wo

2019-07-23 08:56发布

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条回答
Evening l夕情丶
2楼-- · 2019-07-23 09:15

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

查看更多
登录 后发表回答