Skip to content

Commit 00ca45c

Browse files
committed
fix: keep React suspense alive without blocking Solid
Keep resolving shared load promises in core so Solid suspense and selective SSR can complete, but let React create a temporary local suspense promise when a pending match no longer has a pending load promise to throw.
1 parent e1dbc02 commit 00ca45c

2 files changed

Lines changed: 48 additions & 12 deletions

File tree

‎packages/react-router/src/Match.tsx‎

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { ScrollRestoration } from './scroll-restoration'
1919
import { ClientOnly } from './ClientOnly'
2020
import 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

‎packages/router-core/src/load-matches.ts‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -839,9 +839,7 @@ const loadRouteMatch = async (
839839
await runLoader(inner, matchPromises, matchId, index, route)
840840
const match = inner.router.getMatch(matchId)!
841841
match._nonReactive.loaderPromise?.resolve()
842-
if (match.status !== 'pending') {
843-
match._nonReactive.loadPromise?.resolve()
844-
}
842+
match._nonReactive.loadPromise?.resolve()
845843
match._nonReactive.loaderPromise = undefined
846844
} catch (err) {
847845
if (isRedirect(err)) {
@@ -940,9 +938,7 @@ const loadRouteMatch = async (
940938
const match = inner.router.getMatch(matchId)!
941939
if (!loaderIsRunningAsync) {
942940
match._nonReactive.loaderPromise?.resolve()
943-
if (match.status !== 'pending') {
944-
match._nonReactive.loadPromise?.resolve()
945-
}
941+
match._nonReactive.loadPromise?.resolve()
946942
}
947943

948944
clearTimeout(match._nonReactive.pendingTimeout)

0 commit comments

Comments
 (0)