Skip to content
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

Merged

Conversation

DrFrankenstein
Copy link

@DrFrankenstein DrFrankenstein commented Dec 7, 2019

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.

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)
Copy link

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?

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)
Copy link

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)
}
Copy link

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
  }
}

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.

@nolanlawson
Copy link

LGTM! Thanks

@nolanlawson nolanlawson merged commit f3387cd into missive:master Dec 21, 2019
@nolanlawson nolanlawson mentioned this pull request Dec 21, 2019
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.

None yet

3 participants