diff --git a/.changeset/flat-emus-repeat.md b/.changeset/flat-emus-repeat.md new file mode 100644 index 00000000000..90a0c403d89 --- /dev/null +++ b/.changeset/flat-emus-repeat.md @@ -0,0 +1,5 @@ +--- +"thirdweb": patch +--- + +Catch localStorage getItem and setItem unhandled errors diff --git a/apps/dashboard/src/hooks/useLocalStorage.ts b/apps/dashboard/src/hooks/useLocalStorage.ts index fb1414287b0..59694e0a1d5 100644 --- a/apps/dashboard/src/hooks/useLocalStorage.ts +++ b/apps/dashboard/src/hooks/useLocalStorage.ts @@ -13,15 +13,22 @@ export function useLocalStorage( // FIXME: ideally we do not need localstorage like this, alernatively we move this into use-query and use-mutation to invalidate etc // eslint-disable-next-line no-restricted-syntax useEffect(() => { - const item = window.localStorage.getItem(key); - - _setValue(item ? JSON.parse(item) : initialValue); + try { + const item = window.localStorage.getItem(key); + _setValue(item ? JSON.parse(item) : initialValue); + } catch { + // ignore + } }, [key, initialValue]); const setValue = (value_: TType) => { _setValue(value_); if (isBrowser()) { - window.localStorage.setItem(key, JSON.stringify(value_)); + try { + window.localStorage.setItem(key, JSON.stringify(value_)); + } catch { + // ignore + } } }; diff --git a/apps/playground-web/src/app/connect/sign-in/button/RightSection.tsx b/apps/playground-web/src/app/connect/sign-in/button/RightSection.tsx index ddf9057cbb9..49fe10319bf 100644 --- a/apps/playground-web/src/app/connect/sign-in/button/RightSection.tsx +++ b/apps/playground-web/src/app/connect/sign-in/button/RightSection.tsx @@ -41,7 +41,11 @@ export function RightSection(props: { // fake login for playground const playgroundAuth: ConnectButtonProps["auth"] = { async doLogin() { - localStorage.setItem("playground-loggedin", "true"); + try { + localStorage.setItem("playground-loggedin", "true"); + } catch { + // ignore + } }, async doLogout() { localStorage.removeItem("playground-loggedin"); @@ -59,7 +63,11 @@ export function RightSection(props: { }; }, async isLoggedIn() { - return !!localStorage.getItem("playground-loggedin"); + try { + return !!localStorage.getItem("playground-loggedin"); + } catch { + return false; + } }, }; diff --git a/apps/portal/src/components/others/Banner.tsx b/apps/portal/src/components/others/Banner.tsx index 8866ac26d9b..9409b7b4612 100644 --- a/apps/portal/src/components/others/Banner.tsx +++ b/apps/portal/src/components/others/Banner.tsx @@ -10,8 +10,12 @@ export function Banner(props: { text: string; href: string; id: string }) { const bannerCancelledKey = `banner-cancelled${props.href}`; useEffect(() => { - if (localStorage.getItem(bannerCancelledKey) !== "true") { - setShowBanner(true); + try { + if (localStorage.getItem(bannerCancelledKey) !== "true") { + setShowBanner(true); + } + } catch { + // ignore } }, [bannerCancelledKey]); @@ -41,7 +45,11 @@ export function Banner(props: { text: string; href: string; id: string }) { className="absolute right-4 shrink-0 bg-none bg-transparent p-1" onClick={() => { setShowBanner(false); - localStorage.setItem(bannerCancelledKey, "true"); + try { + localStorage.setItem(bannerCancelledKey, "true"); + } catch { + // ignore + } }} >