condenser icon indicating copy to clipboard operation
condenser copied to clipboard

3700 carefully access local storage

Open gl2748 opened this issue 6 years ago • 3 comments

Closes #3700

gl2748 avatar Feb 21 '20 13:02 gl2748

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?

roadscape avatar Feb 27 '20 17:02 roadscape

@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

quochuy avatar Feb 27 '20 23:02 quochuy

@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 ...

gl2748 avatar Feb 28 '20 12:02 gl2748