Skip to content

Commit ea6d2d1

Browse files
VugarAhmadovclaude
andcommitted
fix(router-plugin): only patch the route a module exported during route HMR
Route HMR found the route to patch by id on window.__TSR_ROUTER__. When another app's route module loads on the same page (e.g. a Module Federation remote), its `__root__` or `/` matches the host's route, so the host's route was patched with the remote's options as soon as the remote loaded. Each route module now remembers the first Route it exported (in Vite's persistent hot.data, and through dispose for webpack/Rspack) and only patches the router's route when it is that same object. Fixes #7921 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 57bd1a9 commit ea6d2d1

16 files changed

Lines changed: 316 additions & 74 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 let route HMR patch the route that the module itself exported. Matching by route id alone meant a route module from another app on the page, such as a Module Federation remote whose `__root__` or `/` shares an id with the host's, patched the host's route as soon as it loaded.

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

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

25+
// The router holds the `Route` this module exported first, so only that
26+
// route may be patched. Matching by id alone would also patch a route from
27+
// another module, e.g. a Module Federation remote whose `__root__` or `/`
28+
// shares an id with the host's.
2529
return [
2630
template.statement(
2731
`
2832
if (import.meta.hot) {
2933
const hot = import.meta.hot
3034
const hotData = hot.data ??= {}
3135
const handleRouteUpdate = ${handleRouteUpdateCode}
36+
hotData['tsr-route'] ??= Route
37+
const isOwnRoute = (routeId) =>
38+
typeof window !== 'undefined' &&
39+
window.__TSR_ROUTER__?.routesById?.[routeId] === hotData['tsr-route']
3240
const initialRouteId = ${routeIdFallback} ?? hotData['tsr-route-id']
3341
if (initialRouteId) {
3442
hotData['tsr-route-id'] = initialRouteId
3543
}
36-
const existingRoute =
37-
typeof window !== 'undefined' && initialRouteId
38-
? window.__TSR_ROUTER__?.routesById?.[initialRouteId]
39-
: undefined
40-
if (initialRouteId && existingRoute && existingRoute !== Route) {
44+
if (initialRouteId && hotData['tsr-route'] !== Route && isOwnRoute(initialRouteId)) {
4145
handleRouteUpdate(initialRouteId, Route)
4246
hotData['tsr-route-update-handled'] = Route
4347
}
@@ -51,7 +55,9 @@ if (import.meta.hot) {
5155
delete hotData['tsr-route-update-handled']
5256
return
5357
}
54-
handleRouteUpdate(routeId, newModule.Route)
58+
if (isOwnRoute(routeId)) {
59+
handleRouteUpdate(routeId, newModule.Route)
60+
}
5561
}
5662
})
5763
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ export function createWebpackHmrStatement(
6363
} catch (_err) { /* noop */ }`
6464
: ''
6565

66+
// The router holds the `Route` this module exported first, so only that
67+
// route may be patched. Matching by id alone would also patch a route from
68+
// another module, e.g. a Module Federation remote whose `__root__` or `/`
69+
// shares an id with the host's. Webpack only passes `hot.data` to the next
70+
// evaluation through `dispose`, so the first `Route` is carried there.
6671
return [
6772
template.statement(
6873
`
@@ -73,17 +78,19 @@ if (import.meta.webpackHot) {
7378
if (routeId) {
7479
hotData['tsr-route-id'] = routeId
7580
}
81+
const ownRoute = hotData['tsr-route'] ?? Route
7682
const existingRoute =
7783
typeof window !== 'undefined' && routeId
7884
? window.__TSR_ROUTER__?.routesById?.[routeId]
7985
: undefined
80-
if (routeId && existingRoute && existingRoute !== Route) {
86+
if (routeId && existingRoute === ownRoute && existingRoute !== Route) {
8187
(${handleRouteUpdateCode})(routeId, Route)${reactRefreshCall}
8288
}
8389
hot.dispose((data) => {
8490
if (routeId) {
8591
data['tsr-route-id'] = routeId
8692
}
93+
data['tsr-route'] = ownRoute
8794
})
8895
hot.accept()
8996
}

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,19 @@ if (import.meta.hot) {
8989
newRoute._to = liveRoute._to
9090
}
9191
}
92+
hotData['tsr-route'] ??= Route
93+
const isOwnRoute = (routeId) =>
94+
typeof window !== 'undefined' &&
95+
window.__TSR_ROUTER__?.routesById?.[routeId] === hotData['tsr-route']
9296
const initialRouteId = Route.id ?? hotData['tsr-route-id']
9397
if (initialRouteId) {
9498
hotData['tsr-route-id'] = initialRouteId
9599
}
96-
const existingRoute =
97-
typeof window !== 'undefined' && initialRouteId
98-
? window.__TSR_ROUTER__?.routesById?.[initialRouteId]
99-
: undefined
100-
if (initialRouteId && existingRoute && existingRoute !== Route) {
100+
if (
101+
initialRouteId &&
102+
hotData['tsr-route'] !== Route &&
103+
isOwnRoute(initialRouteId)
104+
) {
101105
handleRouteUpdate(initialRouteId, Route)
102106
hotData['tsr-route-update-handled'] = Route
103107
}
@@ -111,7 +115,9 @@ if (import.meta.hot) {
111115
delete hotData['tsr-route-update-handled']
112116
return
113117
}
114-
handleRouteUpdate(routeId, newModule.Route)
118+
if (isOwnRoute(routeId)) {
119+
handleRouteUpdate(routeId, newModule.Route)
120+
}
115121
}
116122
})
117123
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ if (import.meta.webpackHot) {
3030
if (routeId) {
3131
hotData['tsr-route-id'] = routeId
3232
}
33+
const ownRoute = hotData['tsr-route'] ?? Route
3334
const existingRoute =
3435
typeof window !== 'undefined' && routeId
3536
? window.__TSR_ROUTER__?.routesById?.[routeId]
3637
: undefined
37-
if (routeId && existingRoute && existingRoute !== Route) {
38+
if (routeId && existingRoute === ownRoute && existingRoute !== Route) {
3839
;(function handleRouteUpdate(routeId, newRoute) {
3940
const router = window.__TSR_ROUTER__
4041
const oldRoute = router.routesById[routeId]
@@ -102,6 +103,7 @@ if (import.meta.webpackHot) {
102103
if (routeId) {
103104
data['tsr-route-id'] = routeId
104105
}
106+
data['tsr-route'] = ownRoute
105107
})
106108
hot.accept()
107109
}

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,19 @@ if (import.meta.hot) {
108108
newRoute._to = liveRoute._to
109109
}
110110
}
111+
hotData['tsr-route'] ??= Route
112+
const isOwnRoute = (routeId) =>
113+
typeof window !== 'undefined' &&
114+
window.__TSR_ROUTER__?.routesById?.[routeId] === hotData['tsr-route']
111115
const initialRouteId = Route.id ?? hotData['tsr-route-id']
112116
if (initialRouteId) {
113117
hotData['tsr-route-id'] = initialRouteId
114118
}
115-
const existingRoute =
116-
typeof window !== 'undefined' && initialRouteId
117-
? window.__TSR_ROUTER__?.routesById?.[initialRouteId]
118-
: undefined
119-
if (initialRouteId && existingRoute && existingRoute !== Route) {
119+
if (
120+
initialRouteId &&
121+
hotData['tsr-route'] !== Route &&
122+
isOwnRoute(initialRouteId)
123+
) {
120124
handleRouteUpdate(initialRouteId, Route)
121125
hotData['tsr-route-update-handled'] = Route
122126
}
@@ -130,7 +134,9 @@ if (import.meta.hot) {
130134
delete hotData['tsr-route-update-handled']
131135
return
132136
}
133-
handleRouteUpdate(routeId, newModule.Route)
137+
if (isOwnRoute(routeId)) {
138+
handleRouteUpdate(routeId, newModule.Route)
139+
}
134140
}
135141
})
136142
}

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,19 @@ if (import.meta.hot) {
7474
newRoute._to = liveRoute._to
7575
}
7676
}
77+
hotData['tsr-route'] ??= Route
78+
const isOwnRoute = (routeId) =>
79+
typeof window !== 'undefined' &&
80+
window.__TSR_ROUTER__?.routesById?.[routeId] === hotData['tsr-route']
7781
const initialRouteId = Route.id ?? hotData['tsr-route-id']
7882
if (initialRouteId) {
7983
hotData['tsr-route-id'] = initialRouteId
8084
}
81-
const existingRoute =
82-
typeof window !== 'undefined' && initialRouteId
83-
? window.__TSR_ROUTER__?.routesById?.[initialRouteId]
84-
: undefined
85-
if (initialRouteId && existingRoute && existingRoute !== Route) {
85+
if (
86+
initialRouteId &&
87+
hotData['tsr-route'] !== Route &&
88+
isOwnRoute(initialRouteId)
89+
) {
8690
handleRouteUpdate(initialRouteId, Route)
8791
hotData['tsr-route-update-handled'] = Route
8892
}
@@ -96,7 +100,9 @@ if (import.meta.hot) {
96100
delete hotData['tsr-route-update-handled']
97101
return
98102
}
99-
handleRouteUpdate(routeId, newModule.Route)
103+
if (isOwnRoute(routeId)) {
104+
handleRouteUpdate(routeId, newModule.Route)
105+
}
100106
}
101107
})
102108
}

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,19 @@ if (import.meta.hot) {
9090
newRoute._to = liveRoute._to
9191
}
9292
}
93+
hotData['tsr-route'] ??= Route
94+
const isOwnRoute = (routeId) =>
95+
typeof window !== 'undefined' &&
96+
window.__TSR_ROUTER__?.routesById?.[routeId] === hotData['tsr-route']
9397
const initialRouteId = Route.id ?? hotData['tsr-route-id']
9498
if (initialRouteId) {
9599
hotData['tsr-route-id'] = initialRouteId
96100
}
97-
const existingRoute =
98-
typeof window !== 'undefined' && initialRouteId
99-
? window.__TSR_ROUTER__?.routesById?.[initialRouteId]
100-
: undefined
101-
if (initialRouteId && existingRoute && existingRoute !== Route) {
101+
if (
102+
initialRouteId &&
103+
hotData['tsr-route'] !== Route &&
104+
isOwnRoute(initialRouteId)
105+
) {
102106
handleRouteUpdate(initialRouteId, Route)
103107
hotData['tsr-route-update-handled'] = Route
104108
}
@@ -112,7 +116,9 @@ if (import.meta.hot) {
112116
delete hotData['tsr-route-update-handled']
113117
return
114118
}
115-
handleRouteUpdate(routeId, newModule.Route)
119+
if (isOwnRoute(routeId)) {
120+
handleRouteUpdate(routeId, newModule.Route)
121+
}
116122
}
117123
})
118124
}

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,19 @@ if (import.meta.hot) {
7373
newRoute._to = liveRoute._to
7474
}
7575
}
76+
hotData['tsr-route'] ??= Route
77+
const isOwnRoute = (routeId) =>
78+
typeof window !== 'undefined' &&
79+
window.__TSR_ROUTER__?.routesById?.[routeId] === hotData['tsr-route']
7680
const initialRouteId = Route.id ?? hotData['tsr-route-id']
7781
if (initialRouteId) {
7882
hotData['tsr-route-id'] = initialRouteId
7983
}
80-
const existingRoute =
81-
typeof window !== 'undefined' && initialRouteId
82-
? window.__TSR_ROUTER__?.routesById?.[initialRouteId]
83-
: undefined
84-
if (initialRouteId && existingRoute && existingRoute !== Route) {
84+
if (
85+
initialRouteId &&
86+
hotData['tsr-route'] !== Route &&
87+
isOwnRoute(initialRouteId)
88+
) {
8589
handleRouteUpdate(initialRouteId, Route)
8690
hotData['tsr-route-update-handled'] = Route
8791
}
@@ -95,7 +99,9 @@ if (import.meta.hot) {
9599
delete hotData['tsr-route-update-handled']
96100
return
97101
}
98-
handleRouteUpdate(routeId, newModule.Route)
102+
if (isOwnRoute(routeId)) {
103+
handleRouteUpdate(routeId, newModule.Route)
104+
}
99105
}
100106
})
101107
}

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,19 @@ if (import.meta.hot) {
8181
newRoute._to = liveRoute._to
8282
}
8383
}
84+
hotData['tsr-route'] ??= Route
85+
const isOwnRoute = (routeId) =>
86+
typeof window !== 'undefined' &&
87+
window.__TSR_ROUTER__?.routesById?.[routeId] === hotData['tsr-route']
8488
const initialRouteId = Route.id ?? hotData['tsr-route-id']
8589
if (initialRouteId) {
8690
hotData['tsr-route-id'] = initialRouteId
8791
}
88-
const existingRoute =
89-
typeof window !== 'undefined' && initialRouteId
90-
? window.__TSR_ROUTER__?.routesById?.[initialRouteId]
91-
: undefined
92-
if (initialRouteId && existingRoute && existingRoute !== Route) {
92+
if (
93+
initialRouteId &&
94+
hotData['tsr-route'] !== Route &&
95+
isOwnRoute(initialRouteId)
96+
) {
9397
handleRouteUpdate(initialRouteId, Route)
9498
hotData['tsr-route-update-handled'] = Route
9599
}
@@ -103,7 +107,9 @@ if (import.meta.hot) {
103107
delete hotData['tsr-route-update-handled']
104108
return
105109
}
106-
handleRouteUpdate(routeId, newModule.Route)
110+
if (isOwnRoute(routeId)) {
111+
handleRouteUpdate(routeId, newModule.Route)
112+
}
107113
}
108114
})
109115
}

0 commit comments

Comments
 (0)