Skip to content

Commit 837897f

Browse files
fix(router-plugin): preserve generated route state for aliased HMR imports (#7560)
* fix(router-plugin): preserve generated route state for aliased HMR imports fixes #4303 * ci: apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent df1076c commit 837897f

21 files changed

Lines changed: 1575 additions & 1095 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+
Fix route HMR so aliased route imports keep generated route properties after hot reloads.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Link } from '@tanstack/react-router'
2+
import { Route as parentRoute } from '../routes/aliased-route-imports.$id'
3+
import { Route } from '../routes/aliased-route-imports.$id.child'
4+
5+
export function AliasedRouteImportChildPanel() {
6+
const { id } = Route.useParams()
7+
8+
return (
9+
<main className="hmr-card flex flex-col gap-4">
10+
<p className="hmr-marker" data-testid="aliased-child-marker">
11+
aliased-child-baseline
12+
</p>
13+
<Link
14+
className="hmr-button w-fit"
15+
data-testid="aliased-back-parent"
16+
params={{ id }}
17+
to={parentRoute.to}
18+
>
19+
Back to aliased parent {id}
20+
</Link>
21+
</main>
22+
)
23+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Link, Outlet } from '@tanstack/react-router'
2+
import { Route as childRoute } from '../routes/aliased-route-imports.$id.child'
3+
import { Route } from '../routes/aliased-route-imports.$id'
4+
5+
export function AliasedRouteImportParentPanel() {
6+
const { id } = Route.useParams()
7+
8+
return (
9+
<main className="hmr-card flex flex-col gap-4">
10+
<p className="hmr-marker" data-testid="aliased-parent-marker">
11+
aliased-parent-baseline
12+
</p>
13+
<Link
14+
className="hmr-button w-fit"
15+
data-testid="aliased-show-child"
16+
params={{ id }}
17+
to={childRoute.to}
18+
>
19+
Show aliased child {id}
20+
</Link>
21+
<Outlet />
22+
</main>
23+
)
24+
}

‎e2e/react-start/hmr/src/routeTree.gen.ts‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import { Route as ComponentHmrInlineErrorSplitRouteImport } from './routes/compo
2020
import { Route as ComponentHmrRouteImport } from './routes/component-hmr'
2121
import { Route as ChildRouteImport } from './routes/child'
2222
import { Route as IndexRouteImport } from './routes/index'
23+
import { Route as AliasedRouteImportsIdRouteImport } from './routes/aliased-route-imports.$id'
24+
import { Route as AliasedRouteImportsIdChildRouteImport } from './routes/aliased-route-imports.$id.child'
2325

2426
const ServerFnHmrRoute = ServerFnHmrRouteImport.update({
2527
id: '/server-fn-hmr',
@@ -80,6 +82,17 @@ const IndexRoute = IndexRouteImport.update({
8082
path: '/',
8183
getParentRoute: () => rootRouteImport,
8284
} as any)
85+
const AliasedRouteImportsIdRoute = AliasedRouteImportsIdRouteImport.update({
86+
id: '/aliased-route-imports/$id',
87+
path: '/aliased-route-imports/$id',
88+
getParentRoute: () => rootRouteImport,
89+
} as any)
90+
const AliasedRouteImportsIdChildRoute =
91+
AliasedRouteImportsIdChildRouteImport.update({
92+
id: '/child',
93+
path: '/child',
94+
getParentRoute: () => AliasedRouteImportsIdRoute,
95+
} as any)
8396

8497
export interface FileRoutesByFullPath {
8598
'/': typeof IndexRoute
@@ -93,6 +106,8 @@ export interface FileRoutesByFullPath {
93106
'/component-hmr-named-split': typeof ComponentHmrNamedSplitRoute
94107
'/inputs': typeof InputsRoute
95108
'/server-fn-hmr': typeof ServerFnHmrRoute
109+
'/aliased-route-imports/$id': typeof AliasedRouteImportsIdRouteWithChildren
110+
'/aliased-route-imports/$id/child': typeof AliasedRouteImportsIdChildRoute
96111
}
97112
export interface FileRoutesByTo {
98113
'/': typeof IndexRoute
@@ -106,6 +121,8 @@ export interface FileRoutesByTo {
106121
'/component-hmr-named-split': typeof ComponentHmrNamedSplitRoute
107122
'/inputs': typeof InputsRoute
108123
'/server-fn-hmr': typeof ServerFnHmrRoute
124+
'/aliased-route-imports/$id': typeof AliasedRouteImportsIdRouteWithChildren
125+
'/aliased-route-imports/$id/child': typeof AliasedRouteImportsIdChildRoute
109126
}
110127
export interface FileRoutesById {
111128
__root__: typeof rootRouteImport
@@ -120,6 +137,8 @@ export interface FileRoutesById {
120137
'/component-hmr-named-split': typeof ComponentHmrNamedSplitRoute
121138
'/inputs': typeof InputsRoute
122139
'/server-fn-hmr': typeof ServerFnHmrRoute
140+
'/aliased-route-imports/$id': typeof AliasedRouteImportsIdRouteWithChildren
141+
'/aliased-route-imports/$id/child': typeof AliasedRouteImportsIdChildRoute
123142
}
124143
export interface FileRouteTypes {
125144
fileRoutesByFullPath: FileRoutesByFullPath
@@ -135,6 +154,8 @@ export interface FileRouteTypes {
135154
| '/component-hmr-named-split'
136155
| '/inputs'
137156
| '/server-fn-hmr'
157+
| '/aliased-route-imports/$id'
158+
| '/aliased-route-imports/$id/child'
138159
fileRoutesByTo: FileRoutesByTo
139160
to:
140161
| '/'
@@ -148,6 +169,8 @@ export interface FileRouteTypes {
148169
| '/component-hmr-named-split'
149170
| '/inputs'
150171
| '/server-fn-hmr'
172+
| '/aliased-route-imports/$id'
173+
| '/aliased-route-imports/$id/child'
151174
id:
152175
| '__root__'
153176
| '/'
@@ -161,6 +184,8 @@ export interface FileRouteTypes {
161184
| '/component-hmr-named-split'
162185
| '/inputs'
163186
| '/server-fn-hmr'
187+
| '/aliased-route-imports/$id'
188+
| '/aliased-route-imports/$id/child'
164189
fileRoutesById: FileRoutesById
165190
}
166191
export interface RootRouteChildren {
@@ -175,6 +200,7 @@ export interface RootRouteChildren {
175200
ComponentHmrNamedSplitRoute: typeof ComponentHmrNamedSplitRoute
176201
InputsRoute: typeof InputsRoute
177202
ServerFnHmrRoute: typeof ServerFnHmrRoute
203+
AliasedRouteImportsIdRoute: typeof AliasedRouteImportsIdRouteWithChildren
178204
}
179205

180206
declare module '@tanstack/react-router' {
@@ -256,9 +282,36 @@ declare module '@tanstack/react-router' {
256282
preLoaderRoute: typeof IndexRouteImport
257283
parentRoute: typeof rootRouteImport
258284
}
285+
'/aliased-route-imports/$id': {
286+
id: '/aliased-route-imports/$id'
287+
path: '/aliased-route-imports/$id'
288+
fullPath: '/aliased-route-imports/$id'
289+
preLoaderRoute: typeof AliasedRouteImportsIdRouteImport
290+
parentRoute: typeof rootRouteImport
291+
}
292+
'/aliased-route-imports/$id/child': {
293+
id: '/aliased-route-imports/$id/child'
294+
path: '/child'
295+
fullPath: '/aliased-route-imports/$id/child'
296+
preLoaderRoute: typeof AliasedRouteImportsIdChildRouteImport
297+
parentRoute: typeof AliasedRouteImportsIdRoute
298+
}
259299
}
260300
}
261301

302+
interface AliasedRouteImportsIdRouteChildren {
303+
AliasedRouteImportsIdChildRoute: typeof AliasedRouteImportsIdChildRoute
304+
}
305+
306+
const AliasedRouteImportsIdRouteChildren: AliasedRouteImportsIdRouteChildren = {
307+
AliasedRouteImportsIdChildRoute: AliasedRouteImportsIdChildRoute,
308+
}
309+
310+
const AliasedRouteImportsIdRouteWithChildren =
311+
AliasedRouteImportsIdRoute._addFileChildren(
312+
AliasedRouteImportsIdRouteChildren,
313+
)
314+
262315
const rootRouteChildren: RootRouteChildren = {
263316
IndexRoute: IndexRoute,
264317
ChildRoute: ChildRoute,
@@ -271,6 +324,7 @@ const rootRouteChildren: RootRouteChildren = {
271324
ComponentHmrNamedSplitRoute: ComponentHmrNamedSplitRoute,
272325
InputsRoute: InputsRoute,
273326
ServerFnHmrRoute: ServerFnHmrRoute,
327+
AliasedRouteImportsIdRoute: AliasedRouteImportsIdRouteWithChildren,
274328
}
275329
export const routeTree = rootRouteImport
276330
._addFileChildren(rootRouteChildren)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { createFileRoute } from '@tanstack/react-router'
2+
import { AliasedRouteImportChildPanel } from '../components/AliasedRouteImportChildPanel'
3+
4+
export const Route = createFileRoute('/aliased-route-imports/$id/child')({
5+
component: AliasedRouteImportChildPanel,
6+
})
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { createFileRoute } from '@tanstack/react-router'
2+
import { AliasedRouteImportParentPanel } from '../components/AliasedRouteImportParentPanel'
3+
4+
export const Route = createFileRoute('/aliased-route-imports/$id')({
5+
component: AliasedRouteImportParentPanel,
6+
})

‎e2e/react-start/hmr/tests/app.spec.ts‎

Lines changed: 78 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ const routeFilePaths = {
2727
componentHmrNamedNosplit: 'routes/component-hmr-named-nosplit.tsx',
2828
componentHmrInlineErrorSplit: 'routes/component-hmr-inline-error-split.tsx',
2929
componentHmrNamedErrorSplit: 'routes/component-hmr-named-error-split.tsx',
30+
aliasedRouteImportParentPanel: 'components/AliasedRouteImportParentPanel.tsx',
31+
aliasedRouteImportChildPanel: 'components/AliasedRouteImportChildPanel.tsx',
3032
serverFnHmr: 'routes/server-fn-hmr.tsx',
3133
serverFnHmrFactory: 'hmr/server-fn-hmr-factory.ts',
3234
} as const
@@ -224,6 +226,14 @@ function normalizeRouteSource(routeFileKey: RouteFileKey, source: string) {
224226
'component-hmr-named-error-split-updated',
225227
'component-hmr-named-error-split-baseline',
226228
],
229+
aliasedRouteImportParentPanel: [
230+
'aliased-parent-updated',
231+
'aliased-parent-baseline',
232+
],
233+
aliasedRouteImportChildPanel: [
234+
'aliased-child-updated',
235+
'aliased-child-baseline',
236+
],
227237
}
228238
const markerReplacement = markerReplacements[routeFileKey]
229239
if (markerReplacement) {
@@ -335,7 +345,7 @@ async function waitForServerRenderedText(
335345
url: string,
336346
text: string,
337347
) {
338-
const deadline = Date.now() + 20_000
348+
const deadline = Date.now() + 60_000
339349
let lastError: unknown
340350

341351
while (Date.now() < deadline) {
@@ -354,7 +364,7 @@ async function waitForServerRenderedText(
354364
lastError = error
355365
}
356366

357-
await page.waitForTimeout(150)
367+
await page.waitForTimeout(100)
358368
}
359369

360370
throw lastError
@@ -392,7 +402,7 @@ async function reloadPageAndWaitForText(
392402
testId: string,
393403
text: string,
394404
) {
395-
const deadline = Date.now() + 20_000
405+
const deadline = Date.now() + 60_000
396406
let lastError: unknown
397407

398408
while (Date.now() < deadline) {
@@ -451,7 +461,7 @@ async function waitForRestoredRouteFile(
451461
{ timeout: 500 },
452462
)
453463
await restoreCheck.assert?.(page)
454-
await page.waitForTimeout(500)
464+
await page.waitForTimeout(100)
455465
await expect(page.getByTestId(restoreCheck.testId)).toHaveText(
456466
restoreCheck.text,
457467
{ timeout: 1_000 },
@@ -460,7 +470,7 @@ async function waitForRestoredRouteFile(
460470
return
461471
} catch (error) {
462472
lastError = error
463-
await page.waitForTimeout(150)
473+
await page.waitForTimeout(100)
464474
}
465475
}
466476

@@ -788,6 +798,69 @@ test.describe('react-start hmr', () => {
788798
await expect(page.getByTestId('child')).toHaveText('child')
789799
})
790800

801+
test('keeps aliased route import links working after component HMR', async ({
802+
page,
803+
}) => {
804+
await page.goto('/aliased-route-imports/A')
805+
await page.getByTestId('hydrated').waitFor({ state: 'visible' })
806+
807+
await expect(page.getByTestId('aliased-parent-marker')).toHaveText(
808+
'aliased-parent-baseline',
809+
)
810+
await expect(page.getByTestId('aliased-show-child')).toHaveAttribute(
811+
'href',
812+
'/aliased-route-imports/A/child',
813+
)
814+
815+
await replaceRouteTextAndWait(
816+
page,
817+
'aliasedRouteImportParentPanel',
818+
'aliased-parent-baseline',
819+
'aliased-parent-updated',
820+
async () => {
821+
await hmrExpect(page.getByTestId('aliased-parent-marker')).toHaveText(
822+
'aliased-parent-updated',
823+
)
824+
},
825+
)
826+
827+
await expect(page.getByTestId('aliased-show-child')).toHaveAttribute(
828+
'href',
829+
'/aliased-route-imports/A/child',
830+
)
831+
await page.getByTestId('aliased-show-child').click()
832+
await expect(page).toHaveURL(/\/aliased-route-imports\/A\/child$/)
833+
834+
await page.goto('/aliased-route-imports/A/child')
835+
await page.getByTestId('hydrated').waitFor({ state: 'visible' })
836+
await expect(page.getByTestId('aliased-child-marker')).toHaveText(
837+
'aliased-child-baseline',
838+
)
839+
await expect(page.getByTestId('aliased-back-parent')).toHaveAttribute(
840+
'href',
841+
'/aliased-route-imports/A',
842+
)
843+
844+
await replaceRouteTextAndWait(
845+
page,
846+
'aliasedRouteImportChildPanel',
847+
'aliased-child-baseline',
848+
'aliased-child-updated',
849+
async () => {
850+
await hmrExpect(page.getByTestId('aliased-child-marker')).toHaveText(
851+
'aliased-child-updated',
852+
)
853+
},
854+
)
855+
856+
await expect(page.getByTestId('aliased-back-parent')).toHaveAttribute(
857+
'href',
858+
'/aliased-route-imports/A',
859+
)
860+
await page.getByTestId('aliased-back-parent').click()
861+
await expect(page).toHaveURL(/\/aliased-route-imports\/A$/)
862+
})
863+
791864
test('clears stale beforeLoad context when beforeLoad is removed', async ({
792865
page,
793866
}) => {

‎packages/router-plugin/src/core/hmr/handle-route-update.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type {
77

88
type AnyRouteWithPrivateProps = AnyRoute & {
99
options: Record<string, unknown>
10+
parentRoute: AnyRoute
1011
_componentsPromise?: Promise<void>
1112
_lazyPromise?: Promise<void>
1213
update: (options: Record<string, unknown>) => unknown
@@ -109,6 +110,7 @@ function handleRouteUpdate(
109110
oldRoute._lazyPromise = undefined
110111

111112
router.setRoutes(router.buildRouteTree())
113+
syncHotRouteExport(oldRoute)
112114
router.resolvePathCache.clear()
113115

114116
const filter = (m: AnyRouteMatch) => m.routeId === oldRoute.id
@@ -159,6 +161,18 @@ function handleRouteUpdate(
159161
router.invalidate({ filter, sync: true })
160162
}
161163

164+
function syncHotRouteExport(liveRoute: AnyRouteWithPrivateProps) {
165+
// routeTree.gen.ts mutates the original module export with generated
166+
// routing state. Mirror that state onto the fresh HMR export too, so
167+
// aliased route imports keep working after the module is hot-reloaded.
168+
newRoute.options = liveRoute.options
169+
newRoute.parentRoute = liveRoute.parentRoute
170+
newRoute._path = liveRoute._path
171+
newRoute._id = liveRoute._id
172+
newRoute._fullPath = liveRoute._fullPath
173+
newRoute._to = liveRoute._to
174+
}
175+
162176
function getStoreMatch(matchId: string) {
163177
return (
164178
router.stores.pendingMatchStores.get(matchId)?.get() ||

0 commit comments

Comments
 (0)