Skip to content

Commit

Permalink
fix: Normalize pathnameBase when matching (#8533)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Isammoc committed Feb 27, 2022
1 parent 09aa24f commit 5131627
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions contributors.yml
Expand Up @@ -4,6 +4,7 @@
- chrisngobanh
- elylucas
- hongji00
- Isammoc
- JakubDrozd
- jonkoops
- kddnewton
Expand Down
23 changes: 23 additions & 0 deletions packages/react-router/__tests__/path-matching-test.tsx
Expand Up @@ -242,4 +242,27 @@ describe("path matching with splats", () => {
pathnameBase: "/",
});
});

test("nested routes with partial matching", () => {
let routes = [{ path: '/', children: [{ path: 'courses', children: [{ path: '*' }] }] }];
let match = matchRoutes(routes, "/courses/abc")

expect(match).not.toBeNull();
expect(match).toHaveLength(3);
expect(match[0]).toMatchObject({
params: { "*": "abc" },
pathname: "/",
pathnameBase: "/"
});
expect(match[1]).toMatchObject({
params: { "*": "abc" },
pathname: "/courses",
pathnameBase: "/courses"
});
expect(match[2]).toMatchObject({
params: { "*": "abc" },
pathname: "/courses/abc",
pathnameBase: "/courses"
});
});
});
2 changes: 1 addition & 1 deletion packages/react-router/index.tsx
Expand Up @@ -1040,7 +1040,7 @@ function matchRouteBranch<ParamKey extends string = string>(
matches.push({
params: matchedParams,
pathname: joinPaths([matchedPathname, match.pathname]),
pathnameBase: joinPaths([matchedPathname, match.pathnameBase]),
pathnameBase: normalizePathname(joinPaths([matchedPathname, match.pathnameBase])),
route,
});

Expand Down

0 comments on commit 5131627

Please sign in to comment.