diff --git a/packages/router-core/src/path.ts b/packages/router-core/src/path.ts index e1f6d737dff..20cc564ad2e 100644 --- a/packages/router-core/src/path.ts +++ b/packages/router-core/src/path.ts @@ -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 + } + const value = encodeParam('_splat') if (leaveWildcards) { return `${segmentPrefix}${segment.value}${value ?? ''}${segmentSuffix}` diff --git a/packages/router-core/tests/path.test.ts b/packages/router-core/tests/path.test.ts index 8981f0a85bf..25ac61aa11c 100644 --- a/packages/router-core/tests/path.test.ts +++ b/packages/router-core/tests/path.test.ts @@ -482,7 +482,7 @@ describe('interpolatePath', () => { }) }) - describe('named params (prefix + suffix', () => { + describe('named params (prefix + suffix)', () => { it.each([ { name: 'regular', @@ -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', () => {