@@ -6,31 +6,40 @@ import type { NextClerkProviderProps } from '../../types';
6
6
7
7
declare global {
8
8
interface Window {
9
- __clerk_internal_navFun : NonNullable <
10
- NextClerkProviderProps [ 'routerPush' ] | NextClerkProviderProps [ 'routerReplace' ]
9
+ __clerk_internal_navigations : Record <
10
+ string ,
11
+ {
12
+ fun : NonNullable < NextClerkProviderProps [ 'routerPush' ] | NextClerkProviderProps [ 'routerReplace' ] > ;
13
+ promisesBuffer : Array < ( ) => void > | undefined ;
14
+ }
11
15
> ;
12
- __clerk_internal_navPromisesBuffer : Array < ( ) => void > | undefined ;
13
16
}
14
17
}
15
18
19
+ const getClerkNavigationObject = ( name : string ) => {
20
+ window . __clerk_internal_navigations ??= { } ;
21
+ // @ts -ignore
22
+ window . __clerk_internal_navigations [ name ] ??= { } ;
23
+ return window . __clerk_internal_navigations [ name ] ;
24
+ } ;
25
+
16
26
export const useInternalNavFun = ( props : {
17
27
windowNav : typeof window . history . pushState | typeof window . history . replaceState | undefined ;
18
28
routerNav : AppRouterInstance [ 'push' ] | AppRouterInstance [ 'replace' ] ;
29
+ name : string ;
19
30
} ) => {
20
- const { windowNav, routerNav } = props ;
31
+ const { windowNav, routerNav, name } = props ;
21
32
const pathname = usePathname ( ) ;
22
33
const [ isPending , startTransition ] = useTransition ( ) ;
23
34
24
35
if ( windowNav ) {
25
- window . __clerk_internal_navFun = ( to , opts ) => {
36
+ getClerkNavigationObject ( name ) . fun = ( to , opts ) => {
26
37
return new Promise < void > ( res => {
27
- if ( ! window . __clerk_internal_navPromisesBuffer ) {
28
- // We need to use window to store the reference to the buffer,
29
- // as ClerkProvider might be unmounted and remounted during navigations
30
- // If we use a ref, it will be reset when ClerkProvider is unmounted
31
- window . __clerk_internal_navPromisesBuffer = [ ] ;
32
- }
33
- window . __clerk_internal_navPromisesBuffer . push ( res ) ;
38
+ // We need to use window to store the reference to the buffer,
39
+ // as ClerkProvider might be unmounted and remounted during navigations
40
+ // If we use a ref, it will be reset when ClerkProvider is unmounted
41
+ getClerkNavigationObject ( name ) . promisesBuffer ??= [ ] ;
42
+ getClerkNavigationObject ( name ) . promisesBuffer ?. push ( res ) ;
34
43
startTransition ( ( ) => {
35
44
// If the navigation is internal, we should use the history API to navigate
36
45
// as this is the way to perform a shallow navigation in Next.js App Router
@@ -54,8 +63,8 @@ export const useInternalNavFun = (props: {
54
63
}
55
64
56
65
const flushPromises = ( ) => {
57
- window . __clerk_internal_navPromisesBuffer ?. forEach ( resolve => resolve ( ) ) ;
58
- window . __clerk_internal_navPromisesBuffer = [ ] ;
66
+ getClerkNavigationObject ( name ) . promisesBuffer ?. forEach ( resolve => resolve ( ) ) ;
67
+ getClerkNavigationObject ( name ) . promisesBuffer = [ ] ;
59
68
} ;
60
69
61
70
// Flush any pending promises on mount/unmount
@@ -72,6 +81,8 @@ export const useInternalNavFun = (props: {
72
81
} , [ pathname , isPending ] ) ;
73
82
74
83
return useCallback ( ( to : string ) => {
75
- return window . __clerk_internal_navFun ( to ) ;
84
+ return getClerkNavigationObject ( name ) . fun ( to ) ;
85
+ // We are not expecting name to change
86
+ // eslint-disable-next-line react-hooks/exhaustive-deps
76
87
} , [ ] ) ;
77
88
} ;
0 commit comments