Skip to content

Commit 1718199

Browse files
authored
fix(router-core): empty wilcard node is matched over sibling layout route (#5971)
1 parent d7ba00c commit 1718199

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

packages/router-core/src/new-process-route-tree.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,12 @@ function getNodeMatch<T extends RouteLike>(
10181018
}
10191019
}
10201020

1021+
if (bestMatch && wildcardMatch) {
1022+
return isFrameMoreSpecific(wildcardMatch, bestMatch)
1023+
? bestMatch
1024+
: wildcardMatch
1025+
}
1026+
10211027
if (bestMatch) return bestMatch
10221028

10231029
if (wildcardMatch) return wildcardMatch

packages/router-core/tests/new-process-route-tree.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,30 @@ describe('findRouteMatch', () => {
546546
expect(res?.route.id).toBe('/a/b/$')
547547
expect(res?.params).toEqual({ _splat: 'foo', '*': 'foo' })
548548
})
549+
describe('edge-case #5969: trailing empty wildcard should match', () => {
550+
it('basic', () => {
551+
const tree = makeTree(['/a/$'])
552+
expect(findRouteMatch('/a/', tree)?.route.id).toBe('/a/$')
553+
expect(findRouteMatch('/a', tree)?.route.id).toBe('/a/$')
554+
})
555+
it('with layout route', () => {
556+
const tree = makeTree(['/a', '/a/$'])
557+
expect(findRouteMatch('/a/', tree)?.route.id).toBe('/a/$')
558+
expect(findRouteMatch('/a', tree)?.route.id).toBe('/a/$')
559+
})
560+
it('with index route (should not match)', () => {
561+
const tree = makeTree(['/a/', '/a/$'])
562+
expect(findRouteMatch('/a/', tree)?.route.id).toBe('/a/')
563+
expect(findRouteMatch('/a', tree)?.route.id).toBe('/a/')
564+
})
565+
it('edge-case: deeper index route through skipped optional segments (should not match)', () => {
566+
const tree = makeTree(['/{-$foo}/{-$bar}/a/', '/a/$'])
567+
expect(findRouteMatch('/a/', tree)?.route.id).toBe(
568+
'/{-$foo}/{-$bar}/a/',
569+
)
570+
expect(findRouteMatch('/a', tree)?.route.id).toBe('/{-$foo}/{-$bar}/a/')
571+
})
572+
})
549573
})
550574

551575
describe('nested routes', () => {

0 commit comments

Comments
 (0)