Skip to content

Commit 5131627

Browse files
authored
fix: Normalize pathnameBase when matching (#8533)
Motivation: When matching with "*", the `pathnameBase` had a trailing slash `/`. Modifications: * Call normalizePathname for pathnameBase Result: We can use an element that call useRoutes to define another alternative router for matching with "*". Closes: #8458
1 parent 09aa24f commit 5131627

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

contributors.yml

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
- chrisngobanh
55
- elylucas
66
- hongji00
7+
- Isammoc
78
- JakubDrozd
89
- jonkoops
910
- kddnewton

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

+23
Original file line numberDiff line numberDiff line change
@@ -242,4 +242,27 @@ describe("path matching with splats", () => {
242242
pathnameBase: "/",
243243
});
244244
});
245+
246+
test("nested routes with partial matching", () => {
247+
let routes = [{ path: '/', children: [{ path: 'courses', children: [{ path: '*' }] }] }];
248+
let match = matchRoutes(routes, "/courses/abc")
249+
250+
expect(match).not.toBeNull();
251+
expect(match).toHaveLength(3);
252+
expect(match[0]).toMatchObject({
253+
params: { "*": "abc" },
254+
pathname: "/",
255+
pathnameBase: "/"
256+
});
257+
expect(match[1]).toMatchObject({
258+
params: { "*": "abc" },
259+
pathname: "/courses",
260+
pathnameBase: "/courses"
261+
});
262+
expect(match[2]).toMatchObject({
263+
params: { "*": "abc" },
264+
pathname: "/courses/abc",
265+
pathnameBase: "/courses"
266+
});
267+
});
245268
});

packages/react-router/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1040,7 +1040,7 @@ function matchRouteBranch<ParamKey extends string = string>(
10401040
matches.push({
10411041
params: matchedParams,
10421042
pathname: joinPaths([matchedPathname, match.pathname]),
1043-
pathnameBase: joinPaths([matchedPathname, match.pathnameBase]),
1043+
pathnameBase: normalizePathname(joinPaths([matchedPathname, match.pathnameBase])),
10441044
route,
10451045
});
10461046

0 commit comments

Comments
 (0)