Skip to content

Commit cb44f0c

Browse files
committed
fix(router-plugin): don't patch foreign routers on first route-module import
Since 1.168.17 (837897f, #7560) the injected Vite HMR preamble eagerly calls handleRouteUpdate during the initial evaluation of every route module whenever window.__TSR_ROUTER__ already owns a route with the same id but a different object identity. With multiple routers in one window (module federation host/remote), route ids like __root__ collide between apps and the last-created router wins the global, so the remote's route modules patch the host's router on first import and get the host's tree state written back into their own Route exports — mixing components across the two React copies and triggering "Invalid hook call". hot.data persists across re-evaluations of a module but starts empty on first import, which is exactly the missing discriminator: the eager patch (only needed for the #4303 aliased-import re-evaluation case) is now gated behind a tsr-route-initialized flag in hot.data, restoring pre-1.168.17 behavior for first imports while keeping #4303 fixed. Closes #7921
1 parent 2cb221c commit cb44f0c

14 files changed

Lines changed: 65 additions & 12 deletions
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/router-plugin': patch
3+
---
4+
5+
Only run the eager HMR route patch on hot re-evaluations of a route module. On a first import, a same-id route on `window.__TSR_ROUTER__` belongs to a different router in the same window (e.g. module federation host/remote), and patching it corrupted both route trees, causing "Invalid hook call" errors.

‎packages/router-plugin/src/core/hmr/vite-adapter.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ export function createViteHmrStatement(
2222
const routeIdFallback =
2323
typeof opts.routeId === 'string' ? JSON.stringify(opts.routeId) : 'Route.id'
2424

25+
// `hot.data` persists across re-evaluations of this module but starts empty
26+
// on the very first import. The eager patch below (which mirrors the live
27+
// route's generated state onto this module's `Route` export, see #4303)
28+
// must therefore only run on a hot re-evaluation: on a first import, a
29+
// same-id route found on `window.__TSR_ROUTER__` belongs to a *different*
30+
// router living in the same window (e.g. a module-federation host/remote
31+
// pair), and patching it would graft foreign route options and components
32+
// across the two apps (#7921).
2533
return [
2634
template.statement(
2735
`
@@ -33,8 +41,10 @@ if (import.meta.hot) {
3341
if (initialRouteId) {
3442
hotData['tsr-route-id'] = initialRouteId
3543
}
44+
const isHotReevaluation = hotData['tsr-route-initialized'] === true
45+
hotData['tsr-route-initialized'] = true
3646
const existingRoute =
37-
typeof window !== 'undefined' && initialRouteId
47+
isHotReevaluation && typeof window !== 'undefined' && initialRouteId
3848
? window.__TSR_ROUTER__?.routesById?.[initialRouteId]
3949
: undefined
4050
if (initialRouteId && existingRoute && existingRoute !== Route) {

‎packages/router-plugin/tests/add-hmr.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,22 @@ describe('add-hmr works', () => {
147147
expect(output).toContain('"object":{"type":"Identifier","name":"hotData"}')
148148
})
149149

150+
it('gates the eager live-route patch behind a persisted hot-data flag', async () => {
151+
const statement = createRouteHmrStatement([], {
152+
hmrStyle: 'vite',
153+
targetFramework: 'react',
154+
routeId: '/posts',
155+
})
156+
const output = JSON.stringify(statement)
157+
158+
// On a first import the module must never patch a route owned by a
159+
// different router in the same window (module federation / multiple
160+
// routers, see #7921). The eager patch may only run on hot
161+
// re-evaluations, detected via `hot.data`, which persists across updates.
162+
expect(output).toContain('tsr-route-initialized')
163+
expect(output).toContain('"name":"isHotReevaluation"')
164+
})
165+
150166
it('normalizes the generated root route id for Vite HMR', async () => {
151167
const statement = createRouteHmrStatement([], {
152168
hmrStyle: 'vite',

‎packages/router-plugin/tests/add-hmr/snapshots/react/[email protected]‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,9 @@ if (import.meta.hot) {
172172
if (initialRouteId) {
173173
hotData['tsr-route-id'] = initialRouteId;
174174
}
175-
const existingRoute = typeof window !== 'undefined' && initialRouteId ? window.__TSR_ROUTER__?.routesById?.[initialRouteId] : undefined;
175+
const isHotReevaluation = hotData['tsr-route-initialized'] === true;
176+
hotData['tsr-route-initialized'] = true;
177+
const existingRoute = isHotReevaluation && typeof window !== 'undefined' && initialRouteId ? window.__TSR_ROUTER__?.routesById?.[initialRouteId] : undefined;
176178
if (initialRouteId && existingRoute && existingRoute !== Route) {
177179
handleRouteUpdate(initialRouteId, Route);
178180
hotData['tsr-route-update-handled'] = Route;

‎packages/router-plugin/tests/add-hmr/snapshots/react/[email protected]‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,9 @@ if (import.meta.hot) {
183183
if (initialRouteId) {
184184
hotData['tsr-route-id'] = initialRouteId;
185185
}
186-
const existingRoute = typeof window !== 'undefined' && initialRouteId ? window.__TSR_ROUTER__?.routesById?.[initialRouteId] : undefined;
186+
const isHotReevaluation = hotData['tsr-route-initialized'] === true;
187+
hotData['tsr-route-initialized'] = true;
188+
const existingRoute = isHotReevaluation && typeof window !== 'undefined' && initialRouteId ? window.__TSR_ROUTER__?.routesById?.[initialRouteId] : undefined;
187189
if (initialRouteId && existingRoute && existingRoute !== Route) {
188190
handleRouteUpdate(initialRouteId, Route);
189191
hotData['tsr-route-update-handled'] = Route;

‎packages/router-plugin/tests/add-hmr/snapshots/react/[email protected]‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,9 @@ if (import.meta.hot) {
161161
if (initialRouteId) {
162162
hotData['tsr-route-id'] = initialRouteId;
163163
}
164-
const existingRoute = typeof window !== 'undefined' && initialRouteId ? window.__TSR_ROUTER__?.routesById?.[initialRouteId] : undefined;
164+
const isHotReevaluation = hotData['tsr-route-initialized'] === true;
165+
hotData['tsr-route-initialized'] = true;
166+
const existingRoute = isHotReevaluation && typeof window !== 'undefined' && initialRouteId ? window.__TSR_ROUTER__?.routesById?.[initialRouteId] : undefined;
165167
if (initialRouteId && existingRoute && existingRoute !== Route) {
166168
handleRouteUpdate(initialRouteId, Route);
167169
hotData['tsr-route-update-handled'] = Route;

‎packages/router-plugin/tests/add-hmr/snapshots/react/[email protected]‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,9 @@ if (import.meta.hot) {
179179
if (initialRouteId) {
180180
hotData['tsr-route-id'] = initialRouteId;
181181
}
182-
const existingRoute = typeof window !== 'undefined' && initialRouteId ? window.__TSR_ROUTER__?.routesById?.[initialRouteId] : undefined;
182+
const isHotReevaluation = hotData['tsr-route-initialized'] === true;
183+
hotData['tsr-route-initialized'] = true;
184+
const existingRoute = isHotReevaluation && typeof window !== 'undefined' && initialRouteId ? window.__TSR_ROUTER__?.routesById?.[initialRouteId] : undefined;
183185
if (initialRouteId && existingRoute && existingRoute !== Route) {
184186
handleRouteUpdate(initialRouteId, Route);
185187
hotData['tsr-route-update-handled'] = Route;

‎packages/router-plugin/tests/add-hmr/snapshots/react/[email protected]‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,9 @@ if (import.meta.hot) {
163163
if (initialRouteId) {
164164
hotData['tsr-route-id'] = initialRouteId;
165165
}
166-
const existingRoute = typeof window !== 'undefined' && initialRouteId ? window.__TSR_ROUTER__?.routesById?.[initialRouteId] : undefined;
166+
const isHotReevaluation = hotData['tsr-route-initialized'] === true;
167+
hotData['tsr-route-initialized'] = true;
168+
const existingRoute = isHotReevaluation && typeof window !== 'undefined' && initialRouteId ? window.__TSR_ROUTER__?.routesById?.[initialRouteId] : undefined;
167169
if (initialRouteId && existingRoute && existingRoute !== Route) {
168170
handleRouteUpdate(initialRouteId, Route);
169171
hotData['tsr-route-update-handled'] = Route;

‎packages/router-plugin/tests/add-hmr/snapshots/react/[email protected]‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,9 @@ if (import.meta.hot) {
166166
if (initialRouteId) {
167167
hotData['tsr-route-id'] = initialRouteId;
168168
}
169-
const existingRoute = typeof window !== 'undefined' && initialRouteId ? window.__TSR_ROUTER__?.routesById?.[initialRouteId] : undefined;
169+
const isHotReevaluation = hotData['tsr-route-initialized'] === true;
170+
hotData['tsr-route-initialized'] = true;
171+
const existingRoute = isHotReevaluation && typeof window !== 'undefined' && initialRouteId ? window.__TSR_ROUTER__?.routesById?.[initialRouteId] : undefined;
170172
if (initialRouteId && existingRoute && existingRoute !== Route) {
171173
handleRouteUpdate(initialRouteId, Route);
172174
hotData['tsr-route-update-handled'] = Route;

‎packages/router-plugin/tests/add-hmr/snapshots/react/[email protected]‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,9 @@ if (import.meta.hot) {
160160
if (initialRouteId) {
161161
hotData['tsr-route-id'] = initialRouteId;
162162
}
163-
const existingRoute = typeof window !== 'undefined' && initialRouteId ? window.__TSR_ROUTER__?.routesById?.[initialRouteId] : undefined;
163+
const isHotReevaluation = hotData['tsr-route-initialized'] === true;
164+
hotData['tsr-route-initialized'] = true;
165+
const existingRoute = isHotReevaluation && typeof window !== 'undefined' && initialRouteId ? window.__TSR_ROUTER__?.routesById?.[initialRouteId] : undefined;
164166
if (initialRouteId && existingRoute && existingRoute !== Route) {
165167
handleRouteUpdate(initialRouteId, Route);
166168
hotData['tsr-route-update-handled'] = Route;

0 commit comments

Comments
 (0)