Skip to content

fix: trim undefined from splat routes with no param #4558

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions packages/router-core/src/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,21 @@ export function interpolatePath({
usedParams._splat = params._splat
const segmentPrefix = segment.prefixSegment || ''
const segmentSuffix = segment.suffixSegment || ''

// Check if _splat parameter is missing
if (!('_splat' in params)) {
isMissingParams = true
// For missing splat parameters, just return the prefix and suffix without the wildcard
if (leaveWildcards) {
return `${segmentPrefix}${segment.value}${segmentSuffix}`
}
// If there is a prefix or suffix, return them joined, otherwise omit the segment
if (segmentPrefix || segmentSuffix) {
return `${segmentPrefix}${segmentSuffix}`
}
return undefined
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in which cases does this return undefined still?

}

const value = encodeParam('_splat')
if (leaveWildcards) {
return `${segmentPrefix}${segment.value}${value ?? ''}${segmentSuffix}`
Expand Down
44 changes: 43 additions & 1 deletion packages/router-core/tests/path.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ describe('interpolatePath', () => {
})
})

describe('named params (prefix + suffix', () => {
describe('named params (prefix + suffix)', () => {
it.each([
{
name: 'regular',
Expand Down Expand Up @@ -523,6 +523,48 @@ describe('interpolatePath', () => {
).toBe(result)
})
})

describe('should handle missing _splat parameter for', () => {
it.each([
{
name: 'basic splat route',
path: '/hello/$',
params: {},
expectedResult: '/hello',
},
{
name: 'splat route with prefix',
path: '/hello/prefix{$}',
params: {},
expectedResult: '/hello/prefix',
},
{
name: 'splat route with suffix',
path: '/hello/{$}suffix',
params: {},
expectedResult: '/hello/suffix',
},
{
name: 'splat route with prefix and suffix',
path: '/hello/prefix{$}suffix',
params: {},
expectedResult: '/hello/prefixsuffix',
},
{
name: 'nested splat route',
path: '/users/$id/$',
params: { id: '123' },
expectedResult: '/users/123',
},
])('$name', ({ path, params, expectedResult }) => {
const result = interpolatePath({
path,
params,
})
expect(result.interpolatedPath).toBe(expectedResult)
expect(result.isMissingParams).toBe(true)
})
})
})

describe('matchPathname', () => {
Expand Down