Skip to content

Commit 3a8d27e

Browse files
committed
Fix index path to be more specific than a catch-all.
1 parent e53c695 commit 3a8d27e

3 files changed

Lines changed: 96 additions & 0 deletions

File tree

packages/ra-router-tanstack/src/tanStackRouterProvider.spec.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
NestedResourcesPrecedence,
2828
PathlessLayoutRoutesPriority,
2929
PathlessLayoutRoutesWithEmptyRoute,
30+
PathlessLayoutRoutesWithIndexRoute,
3031
} from './tanStackRouterProvider.stories';
3132
import { tanStackRouterProvider } from './tanStackRouterProvider';
3233

@@ -1523,6 +1524,22 @@ describe('tanStackRouterProvider', () => {
15231524
});
15241525
});
15251526

1527+
it('should match the index route as most specific within pathless layout routes', async () => {
1528+
window.location.hash = '#/posts';
1529+
1530+
render(<PathlessLayoutRoutesWithIndexRoute />);
1531+
1532+
await waitFor(() => {
1533+
expect(screen.getByTestId('posts-page')).toBeInTheDocument();
1534+
});
1535+
1536+
fireEvent.click(screen.getByText('Home (index)'));
1537+
1538+
await waitFor(() => {
1539+
expect(screen.getByTestId('home-page')).toBeInTheDocument();
1540+
});
1541+
});
1542+
15261543
describe('Resource Children (Route as children of Resource)', () => {
15271544
it('should navigate to child routes without matching parent edit route', async () => {
15281545
render(<NestedResourcesPrecedence />);

packages/ra-router-tanstack/src/tanStackRouterProvider.stories.tsx

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,3 +1668,81 @@ export const PathlessLayoutRoutesWithEmptyRoute = () => {
16681668
</RouterProviderContext.Provider>
16691669
);
16701670
};
1671+
1672+
export const PathlessLayoutRoutesWithIndexRoute = () => {
1673+
const { RouterWrapper } = tanStackRouterProvider;
1674+
1675+
return (
1676+
<RouterProviderContext.Provider value={tanStackRouterProvider}>
1677+
<RouterWrapper>
1678+
<Routes>
1679+
<Route
1680+
path="*"
1681+
element={
1682+
<div data-testid="catchall-page">
1683+
Catch-all Page
1684+
</div>
1685+
}
1686+
/>
1687+
<Route
1688+
element={
1689+
<div data-testid="layout-wrapper">
1690+
<h2>Layout Wrapper</h2>
1691+
<nav>
1692+
<LinkBase
1693+
to="/posts"
1694+
style={{ marginRight: 10 }}
1695+
>
1696+
Posts
1697+
</LinkBase>
1698+
<LinkBase to="/comments">Comments</LinkBase>
1699+
</nav>
1700+
<nav>
1701+
<LinkBase
1702+
to="/"
1703+
style={{ marginRight: 10 }}
1704+
>
1705+
Home (index)
1706+
</LinkBase>
1707+
</nav>
1708+
<div
1709+
style={{
1710+
border: '2px solid blue',
1711+
padding: 20,
1712+
marginTop: 10,
1713+
}}
1714+
>
1715+
<RouterOutlet />
1716+
</div>
1717+
</div>
1718+
}
1719+
>
1720+
<Route
1721+
index
1722+
element={
1723+
<div data-testid="home-page">
1724+
Home Page (index)
1725+
</div>
1726+
}
1727+
/>
1728+
<Route
1729+
path="/posts"
1730+
element={
1731+
<div data-testid="posts-page">Posts Page</div>
1732+
}
1733+
/>
1734+
<Route
1735+
path="/comments"
1736+
element={
1737+
<div data-testid="comments-page">
1738+
Comments Page
1739+
</div>
1740+
}
1741+
/>
1742+
</Route>
1743+
</Routes>
1744+
<LocationDisplay />
1745+
</RouterWrapper>
1746+
</RouterProviderContext.Provider>
1747+
);
1748+
};

packages/ra-router-tanstack/src/tanStackRouterProvider.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,7 @@ const Routes = ({ children, location: locationProp }: RouterRoutesProps) => {
744744
childMatch &&
745745
// If no best match yet, or the child route is more specific than the current best, use this one
746746
(!bestMatch ||
747+
childMatch.route.index ||
747748
(bestMatch.route.path !== undefined &&
748749
childMatch.route.path !== undefined &&
749750
isMoreSpecific(

0 commit comments

Comments
 (0)