Skip to content

Commit ea7351a

Browse files
authored
fix: properly support index routes with a path in useResolvedPath (#9486)
* fix: properly support index routes with a path in useResolvedPath * chore: remove duped function * add changeset * look at path directly instead
1 parent 3d0d28c commit ea7351a

File tree

5 files changed

+38
-35
lines changed

5 files changed

+38
-35
lines changed

.changeset/cyan-poems-clean.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"react-router-dom": patch
3+
"@remix-run/router": patch
4+
---
5+
6+
properly support index routes with a path in useResolvedPath

packages/react-router-dom/__tests__/data-browser-router-test.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1737,6 +1737,29 @@ function testDomRouter(
17371737
await new Promise((r) => setTimeout(r, 0));
17381738
assertLocation(testWindow, "/form", "?index");
17391739
});
1740+
1741+
it("handles index routes with a path", async () => {
1742+
let { container } = render(
1743+
<TestDataRouter
1744+
window={getWindow("/foo/bar?a=1#hash")}
1745+
hydrationData={{}}
1746+
>
1747+
<Route path="/">
1748+
<Route path="foo">
1749+
<Route
1750+
index={true}
1751+
path="bar"
1752+
element={<NoActionComponent />}
1753+
/>
1754+
</Route>
1755+
</Route>
1756+
</TestDataRouter>
1757+
);
1758+
1759+
expect(container.querySelector("form")?.getAttribute("action")).toBe(
1760+
"/foo/bar?index&a=1#hash"
1761+
);
1762+
});
17401763
});
17411764

17421765
describe("dynamic routes", () => {

packages/react-router/lib/hooks.tsx

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
parsePath,
2020
resolveTo,
2121
warning,
22+
UNSAFE_getPathContributingMatches as getPathContributingMatches,
2223
} from "@remix-run/router";
2324

2425
import type {
@@ -147,36 +148,6 @@ export interface NavigateFunction {
147148
(delta: number): void;
148149
}
149150

150-
/**
151-
* When processing relative navigation we want to ignore ancestor routes that
152-
* do not contribute to the path, such that index/pathless layout routes don't
153-
* interfere.
154-
*
155-
* For example, when moving a route element into an index route and/or a
156-
* pathless layout route, relative link behavior contained within should stay
157-
* the same. Both of the following examples should link back to the root:
158-
*
159-
* <Route path="/">
160-
* <Route path="accounts" element={<Link to=".."}>
161-
* </Route>
162-
*
163-
* <Route path="/">
164-
* <Route path="accounts">
165-
* <Route element={<AccountsLayout />}> // <-- Does not contribute
166-
* <Route index element={<Link to=".."} /> // <-- Does not contribute
167-
* </Route
168-
* </Route>
169-
* </Route>
170-
*/
171-
function getPathContributingMatches(matches: RouteMatch[]) {
172-
return matches.filter(
173-
(match, index) =>
174-
index === 0 ||
175-
(!match.route.index &&
176-
match.pathnameBase !== matches[index - 1].pathnameBase)
177-
);
178-
}
179-
180151
/**
181152
* Returns an imperative method for changing the location. Used by <Link>s, but
182153
* may also be used by other elements to change the location.

packages/router/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { convertRoutesToDataRoutes } from "./utils";
1+
import { convertRoutesToDataRoutes, getPathContributingMatches } from "./utils";
22

33
export type {
44
ActionFunction,
@@ -79,4 +79,7 @@ export * from "./router";
7979
///////////////////////////////////////////////////////////////////////////////
8080

8181
/** @internal */
82-
export { convertRoutesToDataRoutes as UNSAFE_convertRoutesToDataRoutes };
82+
export {
83+
convertRoutesToDataRoutes as UNSAFE_convertRoutesToDataRoutes,
84+
getPathContributingMatches as UNSAFE_getPathContributingMatches,
85+
};

packages/router/utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,8 @@ function getInvalidPathError(
857857
}
858858

859859
/**
860+
* @private
861+
*
860862
* When processing relative navigation we want to ignore ancestor routes that
861863
* do not contribute to the path, such that index/pathless layout routes don't
862864
* interfere.
@@ -882,9 +884,7 @@ export function getPathContributingMatches<
882884
>(matches: T[]) {
883885
return matches.filter(
884886
(match, index) =>
885-
index === 0 ||
886-
(!match.route.index &&
887-
match.pathnameBase !== matches[index - 1].pathnameBase)
887+
index === 0 || (match.route.path && match.route.path.length > 0)
888888
);
889889
}
890890

0 commit comments

Comments
 (0)