Skip to content

Conversation

@molily
Copy link
Contributor

@molily molily commented Feb 13, 2019

Hello! I’ve configured my browser to block cookies unless allowed per domain. Access to localStorage throws an exception in this case. This blows up many web sites that render content using JS and do not guard the use of localStorage with a try {} catch () {}. Including preactjs.com:

screenshot 2019-02-13 at 09 50 40

This change introduces helper functions for accessing localStorage wrapped in try/catch.

Let me know if you find this useful.

let state;
try {
state = JSON.parse(localStorage.state);
state = JSON.parse(localStorageGet('state'));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is already wrapped in a try-catch, so it was safe already, but i’ve used localStorageGet here for consistency.

Copy link
Member

@developit developit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! This should apply equally to Safari in-private browsing too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants