Skip to content

Commit 5edf465

Browse files
authored
fix(react-router): fix matchPathname with basepath (#2443)
1 parent 661671f commit 5edf465

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

packages/react-router/src/path.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,10 @@ export function matchByPath(
298298
from: string,
299299
matchLocation: Pick<MatchLocation, 'to' | 'caseSensitive' | 'fuzzy'>,
300300
): Record<string, string> | undefined {
301+
// check basepath first
302+
if (basepath !== '/' && !from.startsWith(basepath)) {
303+
return undefined
304+
}
301305
// Remove the base path from the pathname
302306
from = removeBasepath(basepath, from)
303307
// Default to to $ (wildcard)

packages/react-router/tests/path.test.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { describe, expect, it } from 'vitest'
22
import {
33
exactPathTest,
44
interpolatePath,
5+
matchPathname,
56
removeBasepath,
67
removeTrailingSlash,
78
resolvePath,
@@ -276,3 +277,57 @@ describe('interpolatePath', () => {
276277
})
277278
})
278279
})
280+
281+
describe('matchPathname', () => {
282+
it.each([
283+
{
284+
name: 'should match the root path that start with the basepath',
285+
basepath: '/basepath',
286+
pathname: '/basepath',
287+
matchLocation: {
288+
to: '/',
289+
},
290+
expected: {},
291+
},
292+
{
293+
name: 'should match the path that start with the basepath',
294+
basepath: '/basepath',
295+
pathname: '/basepath/abc',
296+
matchLocation: {
297+
to: '/abc',
298+
},
299+
expected: {},
300+
},
301+
{
302+
name: 'should not match the root path that does not start with the basepath',
303+
basepath: '/basepath',
304+
pathname: '/',
305+
matchLocation: {
306+
to: '/',
307+
},
308+
expected: undefined,
309+
},
310+
{
311+
name: 'should not match the path that does not start with the basepath',
312+
basepath: '/basepath',
313+
pathname: '/abc',
314+
matchLocation: {
315+
to: '/abc',
316+
},
317+
expected: undefined,
318+
},
319+
{
320+
name: 'should not match the path that match partial of the basepath',
321+
basepath: '/base',
322+
pathname: '/basepath/abc',
323+
matchLocation: {
324+
to: '/abc',
325+
},
326+
expected: undefined,
327+
},
328+
])('$name', ({ basepath, pathname, matchLocation, expected }) => {
329+
expect(matchPathname(basepath, pathname, matchLocation)).toStrictEqual(
330+
expected,
331+
)
332+
})
333+
})

0 commit comments

Comments
 (0)