-
Notifications
You must be signed in to change notification settings - Fork 842
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: gracefully ignore bad JSON in localstorage #385
fix: gracefully ignore bad JSON in localstorage #385
Conversation
Don't crash (catch the exception) if the localstorage value does not contain valid JSON.
@@ -44,13 +44,13 @@ function get(key) { | |||
if (!isLocalStorageSupported) return | |||
try { | |||
var value = window.localStorage[`${NAMESPACE}.${key}`] | |||
|
|||
if (value) { | |||
return JSON.parse(value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could/should it be _JSON
like in the set function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that _JSON
should be removed entirely; not sure why it's there.
@@ -44,13 +44,13 @@ function get(key) { | |||
if (!isLocalStorageSupported) return | |||
try { | |||
var value = window.localStorage[`${NAMESPACE}.${key}`] | |||
|
|||
if (value) { | |||
return JSON.parse(value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could/should it be _JSON
like in the set function?
|
||
if (value) { | ||
return JSON.parse(value) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Catch as much as possible?
function get(key) {
try {
if (getter) {
return getter(key)
}
if (!isLocalStorageSupported) return
var value = window.localStorage[`${NAMESPACE}.${key}`]
if (value) {
return _JSON.parse(value)
}
} catch (e) {
return
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like a good idea given that on some environments (e.g. iOS Safari in "no cookies" mode) localStorage throws an error if you try to access it.
LGTM! Thanks |
Background
We were previously using an Angular port of emoji-mart, then we migrated our application to React, and began using the original emoji-mart. That port used the
emoji-mart.last
localstorage item slightly differently, by not JSON-encoding it. After the migration, our app would crash while rendering emoji-mart, as it attempted to parse the stale value from our users' localstorage.Changes
This PR changes the store's behaviour to discard invalid JSON values instead of raising an error through
JSON.parse
. That way, if those values ever become corrupt or otherwise invalid for some reason, things like recent emoji just get forgotten, instead of the app inexplicably crashing.