Skip to content

Commit 5e130d7

Browse files
authored
refactor(router-core): interpolatePath skips optional segments w/ nullish param value (#5934)
1 parent bc79c97 commit 5e130d7

File tree

6 files changed

+27
-16
lines changed

6 files changed

+27
-16
lines changed

packages/react-router/tests/navigate.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,7 +1146,7 @@ describe('router.navigate navigation using optional path parameters - edge cases
11461146
})
11471147
await router.invalidate()
11481148

1149-
expect(router.state.location.pathname).toBe('/files/prefix.txt')
1149+
expect(router.state.location.pathname).toBe('/files')
11501150

11511151
// Add the name parameter back
11521152
await router.navigate({
@@ -1236,7 +1236,7 @@ describe('router.navigate navigation using optional path parameters - edge cases
12361236
})
12371237
await router.invalidate()
12381238

1239-
expect(router.state.location.pathname).toBe('/files/prefix.txt')
1239+
expect(router.state.location.pathname).toBe('/files')
12401240
})
12411241
})
12421242

packages/react-router/tests/optional-path-params.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ describe('React Router - Optional Path Parameters', () => {
578578
const filesLink = await screen.findByTestId('files-link')
579579
const docLink = await screen.findByTestId('doc-link')
580580

581-
expect(filesLink).toHaveAttribute('href', '/files/prefix.txt')
581+
expect(filesLink).toHaveAttribute('href', '/files')
582582
expect(docLink).toHaveAttribute('href', '/files/prefixdocument.txt')
583583
})
584584
})

packages/router-core/src/path.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -320,22 +320,15 @@ export function interpolatePath({
320320

321321
if (kind === SEGMENT_TYPE_OPTIONAL_PARAM) {
322322
const key = path.substring(segment[2], segment[3])
323-
const prefix = path.substring(start, segment[1])
324-
const suffix = path.substring(segment[4], end)
325323
const valueRaw = params[key]
326324

327325
// Check if optional parameter is missing or undefined
328-
if (valueRaw == null) {
329-
if (prefix || suffix) {
330-
// For optional params with prefix/suffix, keep the prefix/suffix but omit the param
331-
joined += '/' + prefix + suffix
332-
}
333-
// If no prefix/suffix, omit the entire segment
334-
continue
335-
}
326+
if (valueRaw == null) continue
336327

337328
usedParams[key] = valueRaw
338329

330+
const prefix = path.substring(start, segment[1])
331+
const suffix = path.substring(segment[4], end)
339332
const value = encodeParam(key, params, decodeCharMap) ?? ''
340333
joined += '/' + prefix + value + suffix
341334
continue

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,12 @@ describe('Optional Path Parameters', () => {
205205
name: 'optional param with prefix - omitted',
206206
path: '/posts/prefix{-$category}',
207207
params: {},
208+
result: '/posts',
209+
},
210+
{
211+
name: 'optional param with prefix - empty string',
212+
path: '/posts/prefix{-$category}',
213+
params: { category: '' },
208214
result: '/posts/prefix',
209215
},
210216
{
@@ -217,6 +223,12 @@ describe('Optional Path Parameters', () => {
217223
name: 'optional param with suffix - omitted',
218224
path: '/posts/{-$category}.html',
219225
params: {},
226+
result: '/posts',
227+
},
228+
{
229+
name: 'optional param with suffix - empty string',
230+
path: '/posts/{-$category}.html',
231+
params: { category: '' },
220232
result: '/posts/.html',
221233
},
222234
{
@@ -229,6 +241,12 @@ describe('Optional Path Parameters', () => {
229241
name: 'optional param with prefix and suffix - omitted',
230242
path: '/posts/prefix{-$category}suffix',
231243
params: {},
244+
result: '/posts',
245+
},
246+
{
247+
name: 'optional param with prefix and suffix - empty string',
248+
path: '/posts/prefix{-$category}suffix',
249+
params: { category: '' },
232250
result: '/posts/prefixsuffix',
233251
},
234252
{

packages/solid-router/tests/navigate.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,7 +1170,7 @@ describe('router.navigate navigation using optional path parameters - edge cases
11701170
})
11711171
await router.invalidate()
11721172

1173-
expect(router.state.location.pathname).toBe('/files/prefix.txt')
1173+
expect(router.state.location.pathname).toBe('/files')
11741174

11751175
// Add the name parameter back
11761176
await router.navigate({
@@ -1260,7 +1260,7 @@ describe('router.navigate navigation using optional path parameters - edge cases
12601260
})
12611261
await router.invalidate()
12621262

1263-
expect(router.state.location.pathname).toBe('/files/prefix.txt')
1263+
expect(router.state.location.pathname).toBe('/files')
12641264
})
12651265
})
12661266

packages/solid-router/tests/optional-path-params.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ describe('Solid Router - Optional Path Parameters', () => {
579579
const filesLink = await screen.findByTestId('files-link')
580580
const docLink = await screen.findByTestId('doc-link')
581581

582-
expect(filesLink).toHaveAttribute('href', '/files/prefix.txt')
582+
expect(filesLink).toHaveAttribute('href', '/files')
583583
expect(docLink).toHaveAttribute('href', '/files/prefixdocument.txt')
584584
})
585585
})

0 commit comments

Comments
 (0)