Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trim parent route id prefix when creating lazy routes #2401

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion packages/react-router/src/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1029,7 +1029,7 @@ export class Route<
path = trimPathLeft(path)
}

const customId = path || getLastPathSegment(options?.id)
const customId = path || options?.id

// Strip the parentId prefix from the first level of children
let id = isRoot
Expand Down
13 changes: 13 additions & 0 deletions packages/react-router/tests/lazy/normal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { createLazyFileRoute, createLazyRoute } from '../../src'

export function Route(id: string) {
return createLazyRoute(id)({
component: () => <h1>I'm a normal route</h1>,
})
}

export function FileRoute(id: string) {
return createLazyFileRoute(id as any)({
component: () => <h1>I'm a normal file route</h1>,
})
}
73 changes: 73 additions & 0 deletions packages/react-router/tests/router.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,38 @@ function createTestRouter(initialHistory?: RouterHistory) {
getParentRoute: () => pathSegmentLayoutSplatRoute,
path: '$',
})
const protectedRoute = createRoute({
getParentRoute: () => rootRoute,
id: '/_protected',
}).lazy(() => import('./lazy/normal').then((f) => f.Route('/_protected')))
const protectedLayoutRoute = createRoute({
getParentRoute: () => protectedRoute,
id: '/_layout',
}).lazy(() =>
import('./lazy/normal').then((f) => f.Route('/_protected/_layout')),
)
const protectedFileBasedLayoutRoute = createRoute({
getParentRoute: () => protectedRoute,
id: '/_fileBasedLayout',
}).lazy(() =>
import('./lazy/normal').then((f) =>
f.FileRoute('/_protected/_fileBasedLayout'),
),
)
const protectedFileBasedLayoutTestRoute = createRoute({
getParentRoute: () => protectedFileBasedLayoutRoute,
path: '/fileBasedTest',
}).lazy(() =>
import('./lazy/normal').then((f) =>
f.FileRoute('/_protected/_fileBasedLayout/fileBasedTest'),
),
)
const protectedLayoutTestRoute = createRoute({
getParentRoute: () => protectedLayoutRoute,
path: '/test',
}).lazy(() =>
import('./lazy/normal').then((f) => f.Route('/_protected/_layout/test')),
)

const routeTree = rootRoute.addChildren([
indexRoute,
Expand All @@ -85,6 +117,12 @@ function createTestRouter(initialHistory?: RouterHistory) {
pathSegmentLayoutSplatIndexRoute,
pathSegmentLayoutSplatSplatRoute,
]),
protectedRoute.addChildren([
protectedLayoutRoute.addChildren([protectedLayoutTestRoute]),
protectedFileBasedLayoutRoute.addChildren([
protectedFileBasedLayoutTestRoute,
]),
]),
])

const router = createRouter({ routeTree, history })
Expand Down Expand Up @@ -580,3 +618,38 @@ describe('router matches URLs to route definitions', () => {
])
})
})

describe('route ids should be consistent after rebuilding the route tree', () => {
it('should have the same route ids after rebuilding the route tree', async () => {
const { router } = createTestRouter(
createMemoryHistory({ initialEntries: ['/'] }),
)

const originalRouteIds = Object.keys(router.routesById)

await act(() =>
router.navigate({
to: '/test',
}),
)

await act(() =>
router.navigate({
to: '/fileBasedTest',
}),
)

router.buildRouteTree()

const rebuiltRouteIds = Object.keys(router.routesById)

console.log(originalRouteIds, rebuiltRouteIds)
stevenlyd marked this conversation as resolved.
Show resolved Hide resolved
originalRouteIds.forEach((id) => {
expect(rebuiltRouteIds).toContain(id)
})

// rebuiltRouteIds.forEach((id) => {
// expect(originalRouteIds).toContain(id)
// })
stevenlyd marked this conversation as resolved.
Show resolved Hide resolved
})
})