@@ -6,31 +6,40 @@ import type { NextClerkProviderProps } from '../../types';
66
77declare global {
88 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+ }
1115 > ;
12- __clerk_internal_navPromisesBuffer : Array < ( ) => void > | undefined ;
1316 }
1417}
1518
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+
1626export const useInternalNavFun = ( props : {
1727 windowNav : typeof window . history . pushState | typeof window . history . replaceState | undefined ;
1828 routerNav : AppRouterInstance [ 'push' ] | AppRouterInstance [ 'replace' ] ;
29+ name : string ;
1930} ) => {
20- const { windowNav, routerNav } = props ;
31+ const { windowNav, routerNav, name } = props ;
2132 const pathname = usePathname ( ) ;
2233 const [ isPending , startTransition ] = useTransition ( ) ;
2334
2435 if ( windowNav ) {
25- window . __clerk_internal_navFun = ( to , opts ) => {
36+ getClerkNavigationObject ( name ) . fun = ( to , opts ) => {
2637 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 ) ;
3443 startTransition ( ( ) => {
3544 // If the navigation is internal, we should use the history API to navigate
3645 // as this is the way to perform a shallow navigation in Next.js App Router
@@ -54,8 +63,8 @@ export const useInternalNavFun = (props: {
5463 }
5564
5665 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 = [ ] ;
5968 } ;
6069
6170 // Flush any pending promises on mount/unmount
@@ -72,6 +81,8 @@ export const useInternalNavFun = (props: {
7281 } , [ pathname , isPending ] ) ;
7382
7483 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
7687 } , [ ] ) ;
7788} ;
0 commit comments