Skip to content

Commit af7d038

Browse files
committed
Fix matching a nested absolute splat route
Fixes REM-333
1 parent dcf5c09 commit af7d038

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

packages/react-router/__tests__/absolute-path-matching-test.tsx

+20-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import type { RouteObject } from "react-router";
22
import { matchRoutes } from "react-router";
33

4-
describe("absolute path matching", () => {
5-
function pickPaths(routes: RouteObject[], pathname: string) {
6-
let matches = matchRoutes(routes, { pathname });
7-
return matches ? matches.map(match => match.route.path || "") : [];
8-
}
4+
function pickPaths(routes: RouteObject[], pathname: string): string[] | null {
5+
let matches = matchRoutes(routes, pathname);
6+
return matches && matches.map(match => match.route.path || "");
7+
}
98

9+
describe("absolute path matching", () => {
1010
it("matches a nested route with an absolute path", () => {
1111
let routes = [
1212
{
@@ -26,6 +26,21 @@ describe("absolute path matching", () => {
2626
expect(pickPaths(routes, "/users/123")).toEqual(["/users", "/users/:id"]);
2727
});
2828

29+
it("matches a nested splat route with an absolute path", () => {
30+
let routes = [
31+
{
32+
path: "/users",
33+
children: [{ path: "/users/*" }]
34+
}
35+
];
36+
37+
// expect(pickPaths(routes, "/users")).toEqual(["/users"]);
38+
expect(pickPaths(routes, "/users/not-found")).toEqual([
39+
"/users",
40+
"/users/*"
41+
]);
42+
});
43+
2944
it("throws when the nested path does not begin with its parent path", () => {
3045
expect(() => {
3146
matchRoutes(

packages/react-router/index.tsx

+3-6
Original file line numberDiff line numberDiff line change
@@ -903,16 +903,13 @@ function matchRouteBranch<ParamKey extends string = string>(
903903
let matches: RouteMatch[] = [];
904904
for (let i = 0; i < routesMeta.length; ++i) {
905905
let meta = routesMeta[i];
906+
let end = i === routesMeta.length - 1;
906907
let trailingPathname =
907908
matchedPathname === "/"
908909
? pathname
909910
: pathname.slice(matchedPathname.length) || "/";
910911
let match = matchPath(
911-
{
912-
path: meta.relativePath,
913-
caseSensitive: meta.caseSensitive,
914-
end: i === routesMeta.length - 1
915-
},
912+
{ path: meta.relativePath, caseSensitive: meta.caseSensitive, end },
916913
trailingPathname
917914
);
918915

@@ -1068,7 +1065,7 @@ function compilePath(
10681065
});
10691066

10701067
if (path.endsWith("*")) {
1071-
if (path.endsWith("/*")) {
1068+
if (path !== "/*" && path.endsWith("/*")) {
10721069
source += "(?:\\/(.+)|\\/?)$"; // Don't include the / in params['*']
10731070
} else {
10741071
source += "(.*)$";

0 commit comments

Comments
 (0)