Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/flat-emus-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

Catch localStorage getItem and setItem unhandled errors
15 changes: 11 additions & 4 deletions apps/dashboard/src/hooks/useLocalStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,22 @@ export function useLocalStorage<TType>(
// 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
}
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -59,7 +63,11 @@ export function RightSection(props: {
};
},
async isLoggedIn() {
return !!localStorage.getItem("playground-loggedin");
try {
return !!localStorage.getItem("playground-loggedin");
} catch {
return false;
}
},
};

Expand Down
14 changes: 11 additions & 3 deletions apps/portal/src/components/others/Banner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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]);

Expand Down Expand Up @@ -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
}
}}
>
<XIcon
Expand Down
6 changes: 5 additions & 1 deletion apps/portal/src/components/others/theme/ThemeSwitcher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ export function ThemeSwitcher(props: { className?: string }) {
const newTheme = theme === "light" ? "dark" : "light";
setTheme(newTheme);
document.body.dataset.theme = newTheme;
localStorage.setItem("website-theme", newTheme);
try {
localStorage.setItem("website-theme", newTheme);
} catch {
// ignore
}
}}
variant="outline"
className={cn("p-2", props.className)}
Expand Down
17 changes: 13 additions & 4 deletions packages/thirdweb/src/utils/storage/webStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,23 @@ import type { AsyncStorage } from "./AsyncStorage.js";

export const webLocalStorage: AsyncStorage = {
async getItem(key: string) {
if (typeof window !== "undefined" && window.localStorage) {
return localStorage.getItem(key);
try {
if (typeof window !== "undefined" && window.localStorage) {
return localStorage.getItem(key);
}
} catch {
// ignore
}

return null;
},
async setItem(key: string, value: string) {
if (typeof window !== "undefined" && window.localStorage) {
localStorage.setItem(key, value);
try {
if (typeof window !== "undefined" && window.localStorage) {
localStorage.setItem(key, value);
}
} catch {
// ignore
}
},
async removeItem(key: string) {
Expand Down
Loading