-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't use localStorage when the browser doesn't support it (#971)
* Wrap localStorage with support checks * Add changelog
- Loading branch information
Showing
7 changed files
with
52 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] | ||
} | ||
} |