Skip to content

Commit 8100701

Browse files
committed
fix(tanstack-router): defer mount work past selective-hydration event dispatch
Selective hydration flushes MatchesInner's layout effect in the capture phase of the discrete event that revealed the deferred Suspense arm, before the event reaches its handler. Calling mount() synchronously from that effect lets commitLocation (URL canonicalization) race with the click's own navigation. Defer the stored mount work to a microtask so it runs after event dispatch ends. The Link handler executes first and any in-flight navigation prevents mount from starting a duplicate one.
1 parent dfdeeac commit 8100701

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

‎packages/tanstack-router/src/Matches.tsrx‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,16 @@ function MatchesInner() @{
5858
if (acknowledgement[0] === matches) settleOwner(acknowledgement, true);
5959
}, [acknowledgement, matches]);
6060
// The router's mount work waits for this first commit (useTransitioner).
61+
// Defer the call: selective hydration flushes this effect in the capture
62+
// phase of the discrete event that revealed the arm, before the event
63+
// reaches its handler. A synchronous commitLocation here would race with
64+
// the click's own navigation. A microtask runs after event dispatch ends.
6165
useLayoutEffect(() => {
6266
const routerState = router as any;
6367
routerState._matchesCommitted = true;
6468
const mount = routerState._mountAfterMatches;
6569
routerState._mountAfterMatches = undefined;
66-
mount?.();
70+
if (mount) queueMicrotask(mount);
6771
}, [router]);
6872

6973
<MatchContext value={routeId}>

0 commit comments

Comments
 (0)