Skip to content

Commit

Permalink
fix: more reliable testing of localstorage access (#308)
Browse files Browse the repository at this point in the history
  • Loading branch information
kukhariev authored Jun 15, 2021
1 parent 7d19e9f commit 7fb331c
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/uploadx/lib/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,17 @@ class Store {
}
}

export const store =
typeof window !== 'undefined' && 'localStorage' in window
? new Store()
: new Map<string, string>();
export const store = isLocalStorageAvailable() ? new Store() : new Map<string, string>();

function isLocalStorageAvailable(): boolean {
try {
const key = 'uploadxLocalStorageTest';
const value = 'value';
localStorage.setItem(key, value);
const getValue = localStorage.getItem(key);
localStorage.removeItem(key);
return getValue === value;
} catch {
return false;
}
}

0 comments on commit 7fb331c

Please sign in to comment.