steemit / condenser

The greatest application front-end to the Steem Blockchain.
https://steemit.com
505 stars 429 forks source link

3700 carefully access local storage #3724

Open gl2748 opened 4 years ago

gl2748 commented 4 years ago

Closes #3700

roadscape commented 4 years ago

The issue indicated that the error occurred when trying to read from localStorage. These changes only address writing. Also wondering if the try check may end up being expensive. Is that the recommended approach?

quochuy commented 4 years ago

@roadscape maybe check that localStorage is accessible at a higher level and set a flag, maybe inside a store.

To make it simpler, @gl2748 could also modify AccessLocalStorage() to save a state upon first call instead of doing the try every time

gl2748 commented 4 years ago

@quochuy good idea - to take it one step further - the best practice here is to persist desired slices of the application store/state with the redux subscribe method and to retrieve persisted state once from localStorage at application startup. The most succinct description i've found of this is here:

https://egghead.io/lessons/javascript-redux-persisting-the-state-to-the-local-storage

But unfortunately, we're accessing localStorage all over the place and in inconsistent manner... both reading and writing to it in multiple places - which touches on the point made by @roadscape:

The issue indicated that the error occurred when trying to read from localStorage

Our codebase, failing to be typed as it is, handles this case - when initializing variables from localStorage by accounting for the fact it might be undefined. i.e. it treats read operations from localStorage by a user with privacy settings that preclude access to localStorage in the same way it treats a new visitor to the site, who does not have the desired key being retrieved in localStorage yet. While not a particularly good solution (read operations will throw an uncaught error) variables initialized as undefined from localStorage read operations are widely accounted for in the codebase ...