@@ -19,6 +19,7 @@ import { ScrollRestoration } from './scroll-restoration'
1919import { ClientOnly } from './ClientOnly'
2020import type {
2121 AnyRoute ,
22+ ControlledPromise ,
2223 ParsedLocation ,
2324 RootRouteOptions ,
2425} from '@tanstack/router-core'
@@ -339,6 +340,16 @@ export const MatchInner = React.memo(function MatchInnerImpl({
339340 return out
340341 }
341342
343+ return < MatchInnerClient matchId = { matchId } router = { router } />
344+ } )
345+
346+ function MatchInnerClient ( {
347+ matchId,
348+ router,
349+ } : {
350+ matchId : string
351+ router : ReturnType < typeof useRouter >
352+ } ) {
342353 const matchStore = router . stores . activeMatchStoresById . get ( matchId )
343354 if ( ! matchStore ) {
344355 if ( process . env . NODE_ENV !== 'production' ) {
@@ -349,11 +360,9 @@ export const MatchInner = React.memo(function MatchInnerImpl({
349360
350361 invariant ( )
351362 }
352- // eslint-disable-next-line react-hooks/rules-of-hooks
353363 const match = useStore ( matchStore , ( value ) => value )
354364 const routeId = match . routeId as string
355365 const route = router . routesById [ routeId ] as AnyRoute
356- // eslint-disable-next-line react-hooks/rules-of-hooks
357366 const key = React . useMemo ( ( ) => {
358367 const remountFn =
359368 ( router . routesById [ routeId ] as AnyRoute ) . options . remountDeps ??
@@ -374,7 +383,6 @@ export const MatchInner = React.memo(function MatchInnerImpl({
374383 router . routesById ,
375384 ] )
376385
377- // eslint-disable-next-line react-hooks/rules-of-hooks
378386 const out = React . useMemo ( ( ) => {
379387 const Comp = route . options . component ?? router . options . defaultComponent
380388 if ( Comp ) {
@@ -383,6 +391,38 @@ export const MatchInner = React.memo(function MatchInnerImpl({
383391 return < Outlet />
384392 } , [ key , route . options . component , router . options . defaultComponent ] )
385393
394+ const suspensePromiseRef = React . useRef < ControlledPromise < void > | undefined > (
395+ undefined ,
396+ )
397+
398+ React . useEffect ( ( ) => {
399+ if ( match . status !== 'pending' && match . status !== 'redirected' ) {
400+ suspensePromiseRef . current ?. resolve ( )
401+ suspensePromiseRef . current = undefined
402+ }
403+ } , [ match . status ] )
404+
405+ React . useEffect ( ( ) => {
406+ return ( ) => {
407+ suspensePromiseRef . current ?. resolve ( )
408+ suspensePromiseRef . current = undefined
409+ }
410+ } , [ match . id ] )
411+
412+ const getSuspensePromise = React . useCallback ( ( ) => {
413+ const loadPromise = router . getMatch ( match . id ) ?. _nonReactive . loadPromise
414+
415+ if ( loadPromise ?. status === 'pending' ) {
416+ return loadPromise
417+ }
418+
419+ if ( ! suspensePromiseRef . current ) {
420+ suspensePromiseRef . current = createControlledPromise < void > ( )
421+ }
422+
423+ return suspensePromiseRef . current
424+ } , [ router , match . id ] )
425+
386426 if ( match . _displayPending ) {
387427 throw router . getMatch ( match . id ) ?. _nonReactive . displayPendingPromise
388428 }
@@ -413,7 +453,7 @@ export const MatchInner = React.memo(function MatchInnerImpl({
413453 }
414454 }
415455 }
416- throw router . getMatch ( match . id ) ?. _nonReactive . loadPromise
456+ throw getSuspensePromise ( )
417457 }
418458
419459 if ( match . status === 'notFound' ) {
@@ -442,7 +482,7 @@ export const MatchInner = React.memo(function MatchInnerImpl({
442482 // false,
443483 // 'Tried to render a redirected route match! This is a weird circumstance, please file an issue!',
444484 // )
445- throw router . getMatch ( match . id ) ?. _nonReactive . loadPromise
485+ throw getSuspensePromise ( )
446486 }
447487
448488 if ( match . status === 'error' ) {
@@ -471,7 +511,7 @@ export const MatchInner = React.memo(function MatchInnerImpl({
471511 }
472512
473513 return out
474- } )
514+ }
475515
476516/**
477517 * Render the next child match in the route tree. Typically used inside
0 commit comments