Skip to content
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
4 changes: 2 additions & 2 deletions packages/react-router/tests/navigate.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1146,7 +1146,7 @@ describe('router.navigate navigation using optional path parameters - edge cases
})
await router.invalidate()

expect(router.state.location.pathname).toBe('/files/prefix.txt')
expect(router.state.location.pathname).toBe('/files')

// Add the name parameter back
await router.navigate({
Expand Down Expand Up @@ -1236,7 +1236,7 @@ describe('router.navigate navigation using optional path parameters - edge cases
})
await router.invalidate()

expect(router.state.location.pathname).toBe('/files/prefix.txt')
expect(router.state.location.pathname).toBe('/files')
})
})

Expand Down
2 changes: 1 addition & 1 deletion packages/react-router/tests/optional-path-params.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ describe('React Router - Optional Path Parameters', () => {
const filesLink = await screen.findByTestId('files-link')
const docLink = await screen.findByTestId('doc-link')

expect(filesLink).toHaveAttribute('href', '/files/prefix.txt')
expect(filesLink).toHaveAttribute('href', '/files')
expect(docLink).toHaveAttribute('href', '/files/prefixdocument.txt')
})
})
Expand Down
13 changes: 3 additions & 10 deletions packages/router-core/src/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,22 +320,15 @@ export function interpolatePath({

if (kind === SEGMENT_TYPE_OPTIONAL_PARAM) {
const key = path.substring(segment[2], segment[3])
const prefix = path.substring(start, segment[1])
const suffix = path.substring(segment[4], end)
const valueRaw = params[key]

// Check if optional parameter is missing or undefined
if (valueRaw == null) {
if (prefix || suffix) {
// For optional params with prefix/suffix, keep the prefix/suffix but omit the param
joined += '/' + prefix + suffix
}
// If no prefix/suffix, omit the entire segment
continue
}
if (valueRaw == null) continue

usedParams[key] = valueRaw

const prefix = path.substring(start, segment[1])
const suffix = path.substring(segment[4], end)
const value = encodeParam(key, params, decodeCharMap) ?? ''
joined += '/' + prefix + value + suffix
continue
Expand Down
18 changes: 18 additions & 0 deletions packages/router-core/tests/optional-path-params.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,12 @@ describe('Optional Path Parameters', () => {
name: 'optional param with prefix - omitted',
path: '/posts/prefix{-$category}',
params: {},
result: '/posts',
},
{
name: 'optional param with prefix - empty string',
path: '/posts/prefix{-$category}',
params: { category: '' },
result: '/posts/prefix',
},
{
Expand All @@ -217,6 +223,12 @@ describe('Optional Path Parameters', () => {
name: 'optional param with suffix - omitted',
path: '/posts/{-$category}.html',
params: {},
result: '/posts',
},
{
name: 'optional param with suffix - empty string',
path: '/posts/{-$category}.html',
params: { category: '' },
result: '/posts/.html',
},
{
Expand All @@ -229,6 +241,12 @@ describe('Optional Path Parameters', () => {
name: 'optional param with prefix and suffix - omitted',
path: '/posts/prefix{-$category}suffix',
params: {},
result: '/posts',
},
{
name: 'optional param with prefix and suffix - empty string',
path: '/posts/prefix{-$category}suffix',
params: { category: '' },
result: '/posts/prefixsuffix',
},
{
Expand Down
4 changes: 2 additions & 2 deletions packages/solid-router/tests/navigate.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1170,7 +1170,7 @@ describe('router.navigate navigation using optional path parameters - edge cases
})
await router.invalidate()

expect(router.state.location.pathname).toBe('/files/prefix.txt')
expect(router.state.location.pathname).toBe('/files')

// Add the name parameter back
await router.navigate({
Expand Down Expand Up @@ -1260,7 +1260,7 @@ describe('router.navigate navigation using optional path parameters - edge cases
})
await router.invalidate()

expect(router.state.location.pathname).toBe('/files/prefix.txt')
expect(router.state.location.pathname).toBe('/files')
})
})

Expand Down
2 changes: 1 addition & 1 deletion packages/solid-router/tests/optional-path-params.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ describe('Solid Router - Optional Path Parameters', () => {
const filesLink = await screen.findByTestId('files-link')
const docLink = await screen.findByTestId('doc-link')

expect(filesLink).toHaveAttribute('href', '/files/prefix.txt')
expect(filesLink).toHaveAttribute('href', '/files')
expect(docLink).toHaveAttribute('href', '/files/prefixdocument.txt')
})
})
Expand Down
Loading