From 681da25a63da970425fefb1b05204cddfac00d0f Mon Sep 17 00:00:00 2001 From: Michael Krebs Date: Fri, 4 Oct 2024 00:50:11 +0200 Subject: [PATCH] fix json.parse on localstorage error --- src/lib/shared/stores/local-storage.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/lib/shared/stores/local-storage.ts b/src/lib/shared/stores/local-storage.ts index 833b326..e5ffb08 100644 --- a/src/lib/shared/stores/local-storage.ts +++ b/src/lib/shared/stores/local-storage.ts @@ -7,6 +7,16 @@ export function createLocalStorage( onChange?: (value: T) => void, ): Writable & { set: (value: T) => void; get: () => T | null; update: (updater: Updater) => 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(storedValue ? JSON.parse(storedValue) : initialValue); function update(updater: Updater): void {