Don't use localStorage when the browser doesn't support it (#971)
* Wrap localStorage with support checks * Add changelogpull/1057/head
parent
6319e7f29d
commit
255b4b2325
@ -0,0 +1,35 @@
|
||||
// This module checks if localStorage is available and uses it for persistent frontend storage
|
||||
// if possible. Localstorage can be blocked by browsers when people block third-party cookies and
|
||||
// the dashboard is running in embedded mode. In those cases, store stuff in a regular object instead.
|
||||
|
||||
const memStore = {}
|
||||
|
||||
// https://stackoverflow.com/a/16427747
|
||||
function testLocalStorageAvailability(){
|
||||
try {
|
||||
const testItem = 'test';
|
||||
localStorage.setItem(testItem, testItem);
|
||||
localStorage.removeItem(testItem);
|
||||
return true;
|
||||
} catch(e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
const isLocalStorageAvailable = testLocalStorageAvailability()
|
||||
|
||||
export function setItem(key, value) {
|
||||
if (isLocalStorageAvailable) {
|
||||
window.localStorage.setItem(key, value)
|
||||
} else {
|
||||
memStore[key] = value
|
||||
}
|
||||
}
|
||||
|
||||
export function getItem(key) {
|
||||
if (isLocalStorageAvailable) {
|
||||
return window.localStorage.getItem(key)
|
||||
} else {
|
||||
return memStore[key]
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue