Skip to content

Commit c3d3aea

Browse files
committed
fix: trim undefined from splat routes with no param
Fixes #4547
1 parent cd796fa commit c3d3aea

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

packages/router-core/src/path.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,21 @@ export function interpolatePath({
330330
usedParams._splat = params._splat
331331
const segmentPrefix = segment.prefixSegment || ''
332332
const segmentSuffix = segment.suffixSegment || ''
333+
334+
// Check if _splat parameter is missing
335+
if (!('_splat' in params)) {
336+
isMissingParams = true
337+
// For missing splat parameters, just return the prefix and suffix without the wildcard
338+
if (leaveWildcards) {
339+
return `${segmentPrefix}${segment.value}${segmentSuffix}`
340+
}
341+
// If there is a prefix or suffix, return them joined, otherwise omit the segment
342+
if (segmentPrefix || segmentSuffix) {
343+
return `${segmentPrefix}${segmentSuffix}`
344+
}
345+
return undefined
346+
}
347+
333348
const value = encodeParam('_splat')
334349
if (leaveWildcards) {
335350
return `${segmentPrefix}${segment.value}${value ?? ''}${segmentSuffix}`

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

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ describe('interpolatePath', () => {
482482
})
483483
})
484484

485-
describe('named params (prefix + suffix', () => {
485+
describe('named params (prefix + suffix)', () => {
486486
it.each([
487487
{
488488
name: 'regular',
@@ -523,6 +523,48 @@ describe('interpolatePath', () => {
523523
).toBe(result)
524524
})
525525
})
526+
527+
describe('splat route missing parameter issue', () => {
528+
it.each([
529+
{
530+
name: 'should handle missing _splat parameter for basic splat route',
531+
path: '/hello/$',
532+
params: {},
533+
expectedResult: '/hello',
534+
},
535+
{
536+
name: 'should handle missing _splat parameter for splat route with prefix',
537+
path: '/hello/prefix{$}',
538+
params: {},
539+
expectedResult: '/hello/prefix',
540+
},
541+
{
542+
name: 'should handle missing _splat parameter for splat route with suffix',
543+
path: '/hello/{$}suffix',
544+
params: {},
545+
expectedResult: '/hello/suffix',
546+
},
547+
{
548+
name: 'should handle missing _splat parameter for splat route with prefix and suffix',
549+
path: '/hello/prefix{$}suffix',
550+
params: {},
551+
expectedResult: '/hello/prefixsuffix',
552+
},
553+
{
554+
name: 'should handle missing _splat parameter for nested splat route',
555+
path: '/users/$id/$',
556+
params: { id: '123' },
557+
expectedResult: '/users/123',
558+
},
559+
])('$name', ({ path, params, expectedResult }) => {
560+
const result = interpolatePath({
561+
path,
562+
params,
563+
})
564+
expect(result.interpolatedPath).toBe(expectedResult)
565+
expect(result.isMissingParams).toBe(true)
566+
})
567+
})
526568
})
527569

528570
describe('matchPathname', () => {

0 commit comments

Comments
 (0)