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
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/utils/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.

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?

}
} catch (e) {
return
}

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.

}
}

Expand Down