Skip to content

Commit 8e16dff

Browse files
committed
maybe?
1 parent c07c593 commit 8e16dff

4 files changed

Lines changed: 42 additions & 11 deletions

File tree

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,10 @@ export const MatchInner = React.memo(function MatchInnerImpl({
413413
}
414414
}
415415
}
416-
throw router.getMatch(match.id)?._nonReactive.loadPromise
416+
if (match._nonReactive.pendingRenderPromise?.status !== 'pending') {
417+
match._nonReactive.pendingRenderPromise = createControlledPromise<void>()
418+
}
419+
throw match._nonReactive.pendingRenderPromise
417420
}
418421

419422
if (match.status === 'notFound') {

‎packages/router-core/src/Matches.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ export interface RouteMatch<
145145
/** @internal */
146146
pendingTimeout?: ReturnType<typeof setTimeout>
147147
loadPromise?: ControlledPromise<void>
148+
pendingRenderPromise?: ControlledPromise<void>
148149
displayPendingPromise?: Promise<void>
149150
minPendingPromise?: ControlledPromise<void>
150151
dehydrated?: boolean

‎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)

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

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,13 @@ export type ControllablePromise<T = any> = Promise<T> & {
112112

113113
export type InjectedHtmlEntry = Promise<string>
114114

115+
const clearPendingRenderPromise = (match: AnyRouteMatch | undefined) => {
116+
if (match) {
117+
match._nonReactive.pendingRenderPromise?.resolve()
118+
match._nonReactive.pendingRenderPromise = undefined
119+
}
120+
}
121+
115122
export interface Register {
116123
// Lots of things on here like...
117124
// router
@@ -2475,6 +2482,19 @@ export class RouterCore<
24752482
* or reloads re-run their loaders instead of reusing the failed/not-found data.
24762483
*/
24772484
if (mountPending) {
2485+
const pendingMatchesById = new Map(
2486+
pendingMatches.map((match) => [match.id, match]),
2487+
)
2488+
2489+
currentMatches.forEach((currentMatch) => {
2490+
if (
2491+
pendingMatchesById.get(currentMatch.id)?.status !==
2492+
'pending'
2493+
) {
2494+
clearPendingRenderPromise(currentMatch)
2495+
}
2496+
})
2497+
24782498
this.stores.setActiveMatches(pendingMatches)
24792499
this.stores.setPendingMatches([])
24802500
this.stores.setCachedMatches([
@@ -2634,7 +2654,11 @@ export class RouterCore<
26342654

26352655
const activeMatch = this.stores.activeMatchStoresById.get(id)
26362656
if (activeMatch) {
2637-
activeMatch.setState(updater)
2657+
const next = updater(activeMatch.state)
2658+
if (next.status !== 'pending') {
2659+
clearPendingRenderPromise(activeMatch.state)
2660+
}
2661+
activeMatch.setState(() => next)
26382662
return
26392663
}
26402664

@@ -2696,9 +2720,16 @@ export class RouterCore<
26962720
}
26972721

26982722
this.batch(() => {
2699-
this.stores.setActiveMatches(
2700-
this.stores.activeMatchesSnapshot.state.map(invalidate),
2701-
)
2723+
const activeMatches = this.stores.activeMatchesSnapshot.state
2724+
const nextActiveMatches = activeMatches.map(invalidate)
2725+
2726+
activeMatches.forEach((activeMatch, index) => {
2727+
if (nextActiveMatches[index]?.status !== 'pending') {
2728+
clearPendingRenderPromise(activeMatch)
2729+
}
2730+
})
2731+
2732+
this.stores.setActiveMatches(nextActiveMatches)
27022733
this.stores.setCachedMatches(
27032734
this.stores.cachedMatchesSnapshot.state.map(invalidate),
27042735
)

0 commit comments

Comments
 (0)