@@ -26,9 +26,12 @@ export let idsUpdaterMap: Map<string, { current: string | null }[]> = new Map();
26
26
// This allows us to clean up the idsUpdaterMap when the id is no longer used.
27
27
// Map is a strong reference, so unused ids wouldn't be cleaned up otherwise.
28
28
// This can happen in suspended components where mount/unmount is not called.
29
- let registry = new FinalizationRegistry < string > ( ( heldValue ) => {
30
- idsUpdaterMap . delete ( heldValue ) ;
31
- } ) ;
29
+ let registry ;
30
+ if ( typeof window !== 'undefined' && window . FinalizationRegistry ) {
31
+ registry = new FinalizationRegistry < string > ( ( heldValue ) => {
32
+ idsUpdaterMap . delete ( heldValue ) ;
33
+ } ) ;
34
+ }
32
35
33
36
/**
34
37
* If a default is not provided, generate an id.
@@ -41,7 +44,9 @@ export function useId(defaultId?: string): string {
41
44
let res = useSSRSafeId ( value ) ;
42
45
let cleanupRef = useRef ( null ) ;
43
46
44
- registry . register ( cleanupRef , res ) ;
47
+ if ( registry ) {
48
+ registry . register ( cleanupRef , res ) ;
49
+ }
45
50
46
51
if ( canUseDOM ) {
47
52
const cacheIdRef = idsUpdaterMap . get ( res ) ;
@@ -57,7 +62,9 @@ export function useId(defaultId?: string): string {
57
62
return ( ) => {
58
63
// In Suspense, the cleanup function may be not called
59
64
// when it is though, also remove it from the finalization registry.
60
- registry . unregister ( cleanupRef ) ;
65
+ if ( registry ) {
66
+ registry . unregister ( cleanupRef ) ;
67
+ }
61
68
idsUpdaterMap . delete ( r ) ;
62
69
} ;
63
70
} , [ res ] ) ;
0 commit comments