From 6b3d08e70c6c01c6b804fe8d83f331df7c8fcbc1 Mon Sep 17 00:00:00 2001 From: Preeti Uppuluri Date: Thu, 27 Feb 2025 18:17:32 -0500 Subject: [PATCH 1/4] Supporting navigation to / and basePath when basePath is defined --- src/common/utils/match-route/index.ts | 8 ++-- src/common/utils/match-route/test.ts | 64 +++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 3 deletions(-) diff --git a/src/common/utils/match-route/index.ts b/src/common/utils/match-route/index.ts index f66a5173..f6dabc9f 100644 --- a/src/common/utils/match-route/index.ts +++ b/src/common/utils/match-route/index.ts @@ -26,6 +26,8 @@ const matchRoute = ( queryParams: Query = {}, basePath = '' ) => { + // if navigating to to `/` or just basePath, then do not append basePath + const updatedBasePath = ['/', basePath].includes(pathname) ? '' : basePath; const queryParamObject = typeof queryParams === 'string' ? (qs.parse(queryParams) as Query) @@ -34,7 +36,7 @@ const matchRoute = ( const cachedMatch = matchRouteCache.get( pathname, queryParamObject, - basePath + updatedBasePath ); if (cachedMatch && routes.includes(cachedMatch.route)) return cachedMatch; @@ -43,10 +45,10 @@ const matchRoute = ( routes[i], pathname, queryParamObject, - basePath + updatedBasePath ); if (matchedRoute) { - matchRouteCache.set(pathname, queryParamObject, basePath, matchedRoute); + matchRouteCache.set(pathname, queryParamObject, updatedBasePath, matchedRoute); return matchedRoute; } diff --git a/src/common/utils/match-route/test.ts b/src/common/utils/match-route/test.ts index 32d5e1eb..6107c555 100644 --- a/src/common/utils/match-route/test.ts +++ b/src/common/utils/match-route/test.ts @@ -128,6 +128,70 @@ describe('matchRoute()', () => { route: routeB, }); }); + + it('should ignore basePath when navigating to / without path params', () => { + const routeA = { path: '/', component: Noop }; + const routeB = { path: '/:bar', component: Noop }; + const basePath = '/base'; + expect( + matchRoute( + // @ts-ignore + [routeA, routeB], + '/', + DEFAULT_QUERY_PARAMS, + basePath + ) + ).toMatchObject({ + route: routeA, + }); + + expect( + // @ts-ignore + matchRoute([routeA, routeB], '/abc', DEFAULT_QUERY_PARAMS, basePath) + ).toBeNull(); + + expect( + // @ts-ignore + matchRoute([routeA, routeB], '/base/abc', DEFAULT_QUERY_PARAMS, basePath) + ).toMatchObject({ + route: routeA, + }); + expect( + // @ts-ignore + matchRoute([routeB, routeA], '/base/abc', DEFAULT_QUERY_PARAMS, basePath) + ).toMatchObject({ + route: routeB, + }); + }); + + it('should ignore basePath when navigating to just basePath', () => { + const routeA = { path: '/base', component: Noop }; + const routeB = { path: '/:bar', component: Noop }; + const basePath = '/base'; + expect( + matchRoute( + // @ts-ignore + [routeA, routeB], + '/base', + DEFAULT_QUERY_PARAMS, + basePath + ) + ).toMatchObject({ + route: routeA, + }); + + expect( + // @ts-ignore + matchRoute([routeA, routeB], '/abc', DEFAULT_QUERY_PARAMS, basePath) + ).toBeNull(); + + expect( + // @ts-ignore + matchRoute([routeA, routeB], '/base/abc', DEFAULT_QUERY_PARAMS, basePath) + ).toMatchObject({ + route: routeB, + }); + }); }); describe('query', () => { From 8ca5f321cb0856741e007672e5818719d1565097 Mon Sep 17 00:00:00 2001 From: Preeti Uppuluri Date: Thu, 27 Feb 2025 18:55:10 -0500 Subject: [PATCH 2/4] adding changeset and fixing test --- .changeset/ninety-penguins-speak.md | 5 ++++ src/common/utils/match-route/test.ts | 40 +++++++++++++++++++++------- 2 files changed, 36 insertions(+), 9 deletions(-) create mode 100644 .changeset/ninety-penguins-speak.md diff --git a/.changeset/ninety-penguins-speak.md b/.changeset/ninety-penguins-speak.md new file mode 100644 index 00000000..be2fe192 --- /dev/null +++ b/.changeset/ninety-penguins-speak.md @@ -0,0 +1,5 @@ +--- +"react-resource-router": patch +--- + +Supporting navigation to / and basePath when basePath is defined diff --git a/src/common/utils/match-route/test.ts b/src/common/utils/match-route/test.ts index 6107c555..d9b27c14 100644 --- a/src/common/utils/match-route/test.ts +++ b/src/common/utils/match-route/test.ts @@ -156,9 +156,10 @@ describe('matchRoute()', () => { ).toMatchObject({ route: routeA, }); + expect( // @ts-ignore - matchRoute([routeB, routeA], '/base/abc', DEFAULT_QUERY_PARAMS, basePath) + matchRoute([routeB, routeA], '/base/def', DEFAULT_QUERY_PARAMS, basePath) ).toMatchObject({ route: routeB, }); @@ -166,13 +167,14 @@ describe('matchRoute()', () => { it('should ignore basePath when navigating to just basePath', () => { const routeA = { path: '/base', component: Noop }; - const routeB = { path: '/:bar', component: Noop }; + const routeB = { path: '/', component: Noop }; + const routeC = { path: '/:bar', component: Noop }; const basePath = '/base'; expect( matchRoute( // @ts-ignore - [routeA, routeB], - '/base', + [routeA, routeB, routeC], + basePath, DEFAULT_QUERY_PARAMS, basePath ) @@ -181,16 +183,36 @@ describe('matchRoute()', () => { }); expect( - // @ts-ignore - matchRoute([routeA, routeB], '/abc', DEFAULT_QUERY_PARAMS, basePath) - ).toBeNull(); + matchRoute( + // @ts-ignore + [routeA, routeB, routeC], + '/base/base', + DEFAULT_QUERY_PARAMS, + basePath + ) + ).toMatchObject({ + route: routeA, + }); expect( - // @ts-ignore - matchRoute([routeA, routeB], '/base/abc', DEFAULT_QUERY_PARAMS, basePath) + matchRoute( + // @ts-ignore + [routeC, routeA, routeB], + '/base/', + DEFAULT_QUERY_PARAMS, + basePath + ) ).toMatchObject({ route: routeB, }); + + expect( + // @ts-ignore + matchRoute([routeA, routeC, routeB], '/base/abc', DEFAULT_QUERY_PARAMS, basePath) + ).toMatchObject({ + route: routeC, + }); + }); }); From 775b2659303c03b34b546644e9febffefc8f1e26 Mon Sep 17 00:00:00 2001 From: Preeti Uppuluri Date: Thu, 27 Feb 2025 19:11:48 -0500 Subject: [PATCH 3/4] lint fix --- src/common/utils/match-route/index.ts | 7 ++++++- src/common/utils/match-route/test.ts | 28 ++++++++++++++++++++------- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/common/utils/match-route/index.ts b/src/common/utils/match-route/index.ts index f6dabc9f..3c9a2376 100644 --- a/src/common/utils/match-route/index.ts +++ b/src/common/utils/match-route/index.ts @@ -48,7 +48,12 @@ const matchRoute = ( updatedBasePath ); if (matchedRoute) { - matchRouteCache.set(pathname, queryParamObject, updatedBasePath, matchedRoute); + matchRouteCache.set( + pathname, + queryParamObject, + updatedBasePath, + matchedRoute + ); return matchedRoute; } diff --git a/src/common/utils/match-route/test.ts b/src/common/utils/match-route/test.ts index d9b27c14..aaaeabe9 100644 --- a/src/common/utils/match-route/test.ts +++ b/src/common/utils/match-route/test.ts @@ -151,15 +151,25 @@ describe('matchRoute()', () => { ).toBeNull(); expect( - // @ts-ignore - matchRoute([routeA, routeB], '/base/abc', DEFAULT_QUERY_PARAMS, basePath) + matchRoute( + // @ts-ignore + [routeA, routeB], + '/base/abc', + DEFAULT_QUERY_PARAMS, + basePath + ) ).toMatchObject({ route: routeA, }); expect( - // @ts-ignore - matchRoute([routeB, routeA], '/base/def', DEFAULT_QUERY_PARAMS, basePath) + matchRoute( + // @ts-ignore + [routeB, routeA], + '/base/def', + DEFAULT_QUERY_PARAMS, + basePath + ) ).toMatchObject({ route: routeB, }); @@ -207,12 +217,16 @@ describe('matchRoute()', () => { }); expect( - // @ts-ignore - matchRoute([routeA, routeC, routeB], '/base/abc', DEFAULT_QUERY_PARAMS, basePath) + matchRoute( + // @ts-ignore + [routeA, routeC, routeB], + '/base/abc', + DEFAULT_QUERY_PARAMS, + basePath + ) ).toMatchObject({ route: routeC, }); - }); }); From e5cd5b2a93db7590256e6061978549468bbd7cbe Mon Sep 17 00:00:00 2001 From: Preeti Uppuluri Date: Thu, 27 Feb 2025 19:36:30 -0500 Subject: [PATCH 4/4] Updating changeset to minor --- .changeset/ninety-penguins-speak.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/ninety-penguins-speak.md b/.changeset/ninety-penguins-speak.md index be2fe192..d6f4a160 100644 --- a/.changeset/ninety-penguins-speak.md +++ b/.changeset/ninety-penguins-speak.md @@ -1,5 +1,5 @@ --- -"react-resource-router": patch +"react-resource-router": minor --- Supporting navigation to / and basePath when basePath is defined