Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Supporting navigation to / and basePath when basePath is defined #252

Merged
merged 4 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/ninety-penguins-speak.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-resource-router": minor
---

Supporting navigation to / and basePath when basePath is defined
13 changes: 10 additions & 3 deletions src/common/utils/match-route/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const matchRoute = <T extends Route | InvariantRoute>(
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)
Expand All @@ -34,7 +36,7 @@ const matchRoute = <T extends Route | InvariantRoute>(
const cachedMatch = matchRouteCache.get<T>(
pathname,
queryParamObject,
basePath
updatedBasePath
);
if (cachedMatch && routes.includes(cachedMatch.route)) return cachedMatch;

Expand All @@ -43,10 +45,15 @@ const matchRoute = <T extends Route | InvariantRoute>(
routes[i],
pathname,
queryParamObject,
basePath
updatedBasePath
);
if (matchedRoute) {
matchRouteCache.set(pathname, queryParamObject, basePath, matchedRoute);
matchRouteCache.set(
pathname,
queryParamObject,
updatedBasePath,
matchedRoute
);

return matchedRoute;
}
Expand Down
100 changes: 100 additions & 0 deletions src/common/utils/match-route/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,106 @@ 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(
matchRoute(
// @ts-ignore
[routeA, routeB],
'/base/abc',
DEFAULT_QUERY_PARAMS,
basePath
)
).toMatchObject({
route: routeA,
});

expect(
matchRoute(
// @ts-ignore
[routeB, routeA],
'/base/def',
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: '/', component: Noop };
const routeC = { path: '/:bar', component: Noop };
const basePath = '/base';
expect(
matchRoute(
// @ts-ignore
[routeA, routeB, routeC],
basePath,
DEFAULT_QUERY_PARAMS,
basePath
)
).toMatchObject({
route: routeA,
});

expect(
matchRoute(
// @ts-ignore
[routeA, routeB, routeC],
'/base/base',
DEFAULT_QUERY_PARAMS,
basePath
)
).toMatchObject({
route: routeA,
});

expect(
matchRoute(
// @ts-ignore
[routeC, routeA, routeB],
'/base/',
DEFAULT_QUERY_PARAMS,
basePath
)
).toMatchObject({
route: routeB,
});

expect(
matchRoute(
// @ts-ignore
[routeA, routeC, routeB],
'/base/abc',
DEFAULT_QUERY_PARAMS,
basePath
)
).toMatchObject({
route: routeC,
});
});
});

describe('query', () => {
Expand Down