Skip to content

Commit a40f7aa

Browse files
authored
fix(router-core): matching competing optional routes uses proper fullPath for params extraction (#6015)
* fix(router-core): matching competing optional routes uses proper fullPath for params extraction * remove debug console.log
1 parent e6dc649 commit a40f7aa

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,13 @@ function parseSegments<TRouteLike extends RouteLike>(
332332
// but if there is *also* a layout route at this path, save it as notFound
333333
// we can use it when fuzzy matching to display the NotFound component in the layout route
334334
if (!isIndex) node.notFound = route
335-
if (!node.route || (!node.isIndex && isIndex)) node.route = route
335+
// does the new route take precedence over an existing one?
336+
// yes if previous is not an index route and new one is an index route
337+
if (!node.route || (!node.isIndex && isIndex)) {
338+
node.route = route
339+
// when replacing, replace all attributes that are route-specific (`fullPath` only at the moment)
340+
node.fullPath = route.fullPath ?? route.from
341+
}
336342
node.isIndex ||= isIndex
337343
}
338344
}

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,37 @@ describe('findRouteMatch', () => {
831831
expect(result?.params).toEqual({ '**': decoded })
832832
})
833833
})
834+
describe('edge-cases', () => {
835+
it('#6012: optional index at root with param extraction', () => {
836+
const tree = {
837+
id: '__root__',
838+
fullPath: '/',
839+
path: '/',
840+
isRoot: true,
841+
options: {},
842+
children: [
843+
{
844+
id: '/{-$year}/{-$month}/{-$day}',
845+
fullPath: '/{-$year}/{-$month}/{-$day}',
846+
path: '{-$year}/{-$month}/{-$day}',
847+
isRoot: false,
848+
options: {},
849+
},
850+
{
851+
id: '/_pathless/{-$language}/',
852+
fullPath: '/{-$language}/',
853+
path: '{-$language}/',
854+
isRoot: false,
855+
options: {},
856+
},
857+
],
858+
}
859+
const { processedTree } = processRouteTree(tree)
860+
const result = findRouteMatch(`/sv`, processedTree)
861+
expect(result?.route.id).toBe('/_pathless/{-$language}/')
862+
expect(result?.params).toEqual({ language: 'sv' })
863+
})
864+
})
834865
})
835866
})
836867

0 commit comments

Comments
 (0)