File tree 6 files changed +55
-14
lines changed
playground-web/src/app/connect/sign-in/button
portal/src/components/others
packages/thirdweb/src/utils/storage 6 files changed +55
-14
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ " thirdweb " : patch
3
+ ---
4
+
5
+ Catch localStorage getItem and setItem unhandled errors
Original file line number Diff line number Diff line change @@ -13,15 +13,22 @@ export function useLocalStorage<TType>(
13
13
// FIXME: ideally we do not need localstorage like this, alernatively we move this into use-query and use-mutation to invalidate etc
14
14
// eslint-disable-next-line no-restricted-syntax
15
15
useEffect ( ( ) => {
16
- const item = window . localStorage . getItem ( key ) ;
17
-
18
- _setValue ( item ? JSON . parse ( item ) : initialValue ) ;
16
+ try {
17
+ const item = window . localStorage . getItem ( key ) ;
18
+ _setValue ( item ? JSON . parse ( item ) : initialValue ) ;
19
+ } catch {
20
+ // ignore
21
+ }
19
22
} , [ key , initialValue ] ) ;
20
23
21
24
const setValue = ( value_ : TType ) => {
22
25
_setValue ( value_ ) ;
23
26
if ( isBrowser ( ) ) {
24
- window . localStorage . setItem ( key , JSON . stringify ( value_ ) ) ;
27
+ try {
28
+ window . localStorage . setItem ( key , JSON . stringify ( value_ ) ) ;
29
+ } catch {
30
+ // ignore
31
+ }
25
32
}
26
33
} ;
27
34
Original file line number Diff line number Diff line change @@ -41,7 +41,11 @@ export function RightSection(props: {
41
41
// fake login for playground
42
42
const playgroundAuth : ConnectButtonProps [ "auth" ] = {
43
43
async doLogin ( ) {
44
- localStorage . setItem ( "playground-loggedin" , "true" ) ;
44
+ try {
45
+ localStorage . setItem ( "playground-loggedin" , "true" ) ;
46
+ } catch {
47
+ // ignore
48
+ }
45
49
} ,
46
50
async doLogout ( ) {
47
51
localStorage . removeItem ( "playground-loggedin" ) ;
@@ -59,7 +63,11 @@ export function RightSection(props: {
59
63
} ;
60
64
} ,
61
65
async isLoggedIn ( ) {
62
- return ! ! localStorage . getItem ( "playground-loggedin" ) ;
66
+ try {
67
+ return ! ! localStorage . getItem ( "playground-loggedin" ) ;
68
+ } catch {
69
+ return false ;
70
+ }
63
71
} ,
64
72
} ;
65
73
Original file line number Diff line number Diff line change @@ -10,8 +10,12 @@ export function Banner(props: { text: string; href: string; id: string }) {
10
10
const bannerCancelledKey = `banner-cancelled${ props . href } ` ;
11
11
12
12
useEffect ( ( ) => {
13
- if ( localStorage . getItem ( bannerCancelledKey ) !== "true" ) {
14
- setShowBanner ( true ) ;
13
+ try {
14
+ if ( localStorage . getItem ( bannerCancelledKey ) !== "true" ) {
15
+ setShowBanner ( true ) ;
16
+ }
17
+ } catch {
18
+ // ignore
15
19
}
16
20
} , [ bannerCancelledKey ] ) ;
17
21
@@ -41,7 +45,11 @@ export function Banner(props: { text: string; href: string; id: string }) {
41
45
className = "absolute right-4 shrink-0 bg-none bg-transparent p-1"
42
46
onClick = { ( ) => {
43
47
setShowBanner ( false ) ;
44
- localStorage . setItem ( bannerCancelledKey , "true" ) ;
48
+ try {
49
+ localStorage . setItem ( bannerCancelledKey , "true" ) ;
50
+ } catch {
51
+ // ignore
52
+ }
45
53
} }
46
54
>
47
55
< XIcon
Original file line number Diff line number Diff line change @@ -26,7 +26,11 @@ export function ThemeSwitcher(props: { className?: string }) {
26
26
const newTheme = theme === "light" ? "dark" : "light" ;
27
27
setTheme ( newTheme ) ;
28
28
document . body . dataset . theme = newTheme ;
29
- localStorage . setItem ( "website-theme" , newTheme ) ;
29
+ try {
30
+ localStorage . setItem ( "website-theme" , newTheme ) ;
31
+ } catch {
32
+ // ignore
33
+ }
30
34
} }
31
35
variant = "outline"
32
36
className = { cn ( "p-2" , props . className ) }
Original file line number Diff line number Diff line change @@ -2,14 +2,23 @@ import type { AsyncStorage } from "./AsyncStorage.js";
2
2
3
3
export const webLocalStorage : AsyncStorage = {
4
4
async getItem ( key : string ) {
5
- if ( typeof window !== "undefined" && window . localStorage ) {
6
- return localStorage . getItem ( key ) ;
5
+ try {
6
+ if ( typeof window !== "undefined" && window . localStorage ) {
7
+ return localStorage . getItem ( key ) ;
8
+ }
9
+ } catch {
10
+ // ignore
7
11
}
12
+
8
13
return null ;
9
14
} ,
10
15
async setItem ( key : string , value : string ) {
11
- if ( typeof window !== "undefined" && window . localStorage ) {
12
- localStorage . setItem ( key , value ) ;
16
+ try {
17
+ if ( typeof window !== "undefined" && window . localStorage ) {
18
+ localStorage . setItem ( key , value ) ;
19
+ }
20
+ } catch {
21
+ // ignore
13
22
}
14
23
} ,
15
24
async removeItem ( key : string ) {
You can’t perform that action at this time.
0 commit comments