mirror of
https://github.com/plausible/analytics.git
synced 2024-12-11 09:25:38 +03:00
255b4b2325
* Wrap localStorage with support checks * Add changelog
36 lines
951 B
JavaScript
36 lines
951 B
JavaScript
// 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]
|
|
}
|
|
}
|