Skip to content

Commit

Permalink
Merge pull request #34 from Ralf-Pauli/fix/local-storage-error
Browse files Browse the repository at this point in the history
fix json.parse on localstorage error
  • Loading branch information
m-krebs authored Oct 3, 2024
2 parents 3c89b32 + 681da25 commit 8b21c09
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/lib/shared/stores/local-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ export function createLocalStorage<T>(
onChange?: (value: T) => void,
): Writable<T> & { set: (value: T) => void; get: () => T | null; update: (updater: Updater<T>) => void } {
const storedValue = browser ? window.localStorage.getItem(key) : null;

if (storedValue) {
try {
JSON.parse(storedValue);
} catch (error) {
if (browser) window.localStorage.removeItem(key);
window.location.reload();
}
}

const store = writable<T>(storedValue ? JSON.parse(storedValue) : initialValue);

function update(updater: Updater<T>): void {
Expand Down

0 comments on commit 8b21c09

Please sign in to comment.