Skip to content

Commit bf5a671

Browse files
authored
Supporting navigation to / and basePath when basePath is defined (#252)
* Supporting navigation to / and basePath when basePath is defined * adding changeset and fixing test * lint fix * Updating changeset to minor
1 parent 8d998ee commit bf5a671

File tree

3 files changed

+115
-3
lines changed

3 files changed

+115
-3
lines changed

.changeset/ninety-penguins-speak.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"react-resource-router": minor
3+
---
4+
5+
Supporting navigation to / and basePath when basePath is defined

src/common/utils/match-route/index.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ const matchRoute = <T extends Route | InvariantRoute>(
2626
queryParams: Query = {},
2727
basePath = ''
2828
) => {
29+
// if navigating to to `/` or just basePath, then do not append basePath
30+
const updatedBasePath = ['/', basePath].includes(pathname) ? '' : basePath;
2931
const queryParamObject =
3032
typeof queryParams === 'string'
3133
? (qs.parse(queryParams) as Query)
@@ -34,7 +36,7 @@ const matchRoute = <T extends Route | InvariantRoute>(
3436
const cachedMatch = matchRouteCache.get<T>(
3537
pathname,
3638
queryParamObject,
37-
basePath
39+
updatedBasePath
3840
);
3941
if (cachedMatch && routes.includes(cachedMatch.route)) return cachedMatch;
4042

@@ -43,10 +45,15 @@ const matchRoute = <T extends Route | InvariantRoute>(
4345
routes[i],
4446
pathname,
4547
queryParamObject,
46-
basePath
48+
updatedBasePath
4749
);
4850
if (matchedRoute) {
49-
matchRouteCache.set(pathname, queryParamObject, basePath, matchedRoute);
51+
matchRouteCache.set(
52+
pathname,
53+
queryParamObject,
54+
updatedBasePath,
55+
matchedRoute
56+
);
5057

5158
return matchedRoute;
5259
}

src/common/utils/match-route/test.ts

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,106 @@ describe('matchRoute()', () => {
128128
route: routeB,
129129
});
130130
});
131+
132+
it('should ignore basePath when navigating to / without path params', () => {
133+
const routeA = { path: '/', component: Noop };
134+
const routeB = { path: '/:bar', component: Noop };
135+
const basePath = '/base';
136+
expect(
137+
matchRoute(
138+
// @ts-ignore
139+
[routeA, routeB],
140+
'/',
141+
DEFAULT_QUERY_PARAMS,
142+
basePath
143+
)
144+
).toMatchObject({
145+
route: routeA,
146+
});
147+
148+
expect(
149+
// @ts-ignore
150+
matchRoute([routeA, routeB], '/abc', DEFAULT_QUERY_PARAMS, basePath)
151+
).toBeNull();
152+
153+
expect(
154+
matchRoute(
155+
// @ts-ignore
156+
[routeA, routeB],
157+
'/base/abc',
158+
DEFAULT_QUERY_PARAMS,
159+
basePath
160+
)
161+
).toMatchObject({
162+
route: routeA,
163+
});
164+
165+
expect(
166+
matchRoute(
167+
// @ts-ignore
168+
[routeB, routeA],
169+
'/base/def',
170+
DEFAULT_QUERY_PARAMS,
171+
basePath
172+
)
173+
).toMatchObject({
174+
route: routeB,
175+
});
176+
});
177+
178+
it('should ignore basePath when navigating to just basePath', () => {
179+
const routeA = { path: '/base', component: Noop };
180+
const routeB = { path: '/', component: Noop };
181+
const routeC = { path: '/:bar', component: Noop };
182+
const basePath = '/base';
183+
expect(
184+
matchRoute(
185+
// @ts-ignore
186+
[routeA, routeB, routeC],
187+
basePath,
188+
DEFAULT_QUERY_PARAMS,
189+
basePath
190+
)
191+
).toMatchObject({
192+
route: routeA,
193+
});
194+
195+
expect(
196+
matchRoute(
197+
// @ts-ignore
198+
[routeA, routeB, routeC],
199+
'/base/base',
200+
DEFAULT_QUERY_PARAMS,
201+
basePath
202+
)
203+
).toMatchObject({
204+
route: routeA,
205+
});
206+
207+
expect(
208+
matchRoute(
209+
// @ts-ignore
210+
[routeC, routeA, routeB],
211+
'/base/',
212+
DEFAULT_QUERY_PARAMS,
213+
basePath
214+
)
215+
).toMatchObject({
216+
route: routeB,
217+
});
218+
219+
expect(
220+
matchRoute(
221+
// @ts-ignore
222+
[routeA, routeC, routeB],
223+
'/base/abc',
224+
DEFAULT_QUERY_PARAMS,
225+
basePath
226+
)
227+
).toMatchObject({
228+
route: routeC,
229+
});
230+
});
131231
});
132232

133233
describe('query', () => {

0 commit comments

Comments
 (0)