-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. Could/should it be |
||
} | ||
} catch (e) { | ||
return | ||
} | ||
|
||
if (value) { | ||
return JSON.parse(value) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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. |
||
} | ||
} | ||
|
||
|
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.