问题
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