wrap localStorage access in try-catch
Many browsers may support localStorage, but disable access to it in certain situations. For example, Safari private browsing mode will raise an exception if you try to access localStorage from a third-party domain, resulting in unhandled errors like... > QuotaExceededError: DOM Exception 22: An attempt was made to add something to storage that exceeded the quota. The easiest way to handle this for optional features is to simply try-catch all access to localStorage.release
parent
1482050298
commit
c3aae4fb04
|
@ -12,12 +12,19 @@ function update(state) {
|
||||||
|
|
||||||
function set(key, value) {
|
function set(key, value) {
|
||||||
if (!isLocalStorageSupported) return
|
if (!isLocalStorageSupported) return
|
||||||
window.localStorage[`${NAMESPACE}.${key}`] = JSON.stringify(value)
|
try {
|
||||||
|
window.localStorage[`${NAMESPACE}.${key}`] = JSON.stringify(value)
|
||||||
|
} catch (e) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function get(key) {
|
function get(key) {
|
||||||
if (!isLocalStorageSupported) return
|
if (!isLocalStorageSupported) return
|
||||||
var value = window.localStorage[`${NAMESPACE}.${key}`]
|
try {
|
||||||
|
var value = window.localStorage[`${NAMESPACE}.${key}`]
|
||||||
|
} catch (e) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if (value) {
|
if (value) {
|
||||||
return JSON.parse(value)
|
return JSON.parse(value)
|
||||||
|
|
Loading…
Reference in New Issue