From 51316274272024f3301e99092134f75ef0b35218 Mon Sep 17 00:00:00 2001 From: Isammoc Date: Sun, 27 Feb 2022 21:34:54 +0100 Subject: [PATCH] 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 --- contributors.yml | 1 + .../__tests__/path-matching-test.tsx | 23 +++++++++++++++++++ packages/react-router/index.tsx | 2 +- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/contributors.yml b/contributors.yml index 49f728fbcc..dc6610e349 100644 --- a/contributors.yml +++ b/contributors.yml @@ -4,6 +4,7 @@ - chrisngobanh - elylucas - hongji00 +- Isammoc - JakubDrozd - jonkoops - kddnewton diff --git a/packages/react-router/__tests__/path-matching-test.tsx b/packages/react-router/__tests__/path-matching-test.tsx index f23b68d6cd..d9d35cb5ca 100644 --- a/packages/react-router/__tests__/path-matching-test.tsx +++ b/packages/react-router/__tests__/path-matching-test.tsx @@ -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" + }); + }); }); diff --git a/packages/react-router/index.tsx b/packages/react-router/index.tsx index da7e50615b..e7da3a5b7a 100644 --- a/packages/react-router/index.tsx +++ b/packages/react-router/index.tsx @@ -1040,7 +1040,7 @@ function matchRouteBranch( matches.push({ params: matchedParams, pathname: joinPaths([matchedPathname, match.pathname]), - pathnameBase: joinPaths([matchedPathname, match.pathnameBase]), + pathnameBase: normalizePathname(joinPaths([matchedPathname, match.pathnameBase])), route, });