diff --git a/packages/react-router/src/path.ts b/packages/react-router/src/path.ts index 545d581e98..ee9447efb5 100644 --- a/packages/react-router/src/path.ts +++ b/packages/react-router/src/path.ts @@ -203,21 +203,33 @@ export function interpolatePath({ leaveParams, }: InterpolatePathOptions) { const interpolatedPathSegments = parsePathname(path) + const encodedParams: any = {} + + for (const [key, value] of Object.entries(params)) { + const isValueString = typeof value === 'string' + + if (['*', '_splat'].includes(key)) { + // the splat/catch-all routes shouldn't have the '/' encoded out + encodedParams[key] = isValueString ? encodeURI(value) : value + } else { + encodedParams[key] = isValueString ? encodeURIComponent(value) : value + } + } return joinPaths( interpolatedPathSegments.map((segment) => { if (segment.type === 'wildcard') { - const value = params._splat + const value = encodedParams._splat if (leaveWildcards) return `${segment.value}${value ?? ''}` return value } if (segment.type === 'param') { if (leaveParams) { - const value = params[segment.value] + const value = encodedParams[segment.value] return `${segment.value}${value ?? ''}` } - return params![segment.value.substring(1)] ?? 'undefined' + return encodedParams![segment.value.substring(1)] ?? 'undefined' } return segment.value diff --git a/packages/react-router/src/router.ts b/packages/react-router/src/router.ts index 9523919b7a..021d968404 100644 --- a/packages/react-router/src/router.ts +++ b/packages/react-router/src/router.ts @@ -1170,16 +1170,6 @@ export class Router< }) } - // encode all path params so the generated href is valid and stable - Object.keys(nextParams).forEach((key) => { - if (['*', '_splat'].includes(key)) { - // the splat/catch-all routes shouldn't have the '/' encoded out - nextParams[key] = encodeURI(nextParams[key]) - } else { - nextParams[key] = encodeURIComponent(nextParams[key]) - } - }) - pathname = interpolatePath({ path: pathname, params: nextParams ?? {}, diff --git a/packages/react-router/tests/link.test.tsx b/packages/react-router/tests/link.test.tsx index 74f4468e2b..e6fa2f5229 100644 --- a/packages/react-router/tests/link.test.tsx +++ b/packages/react-router/tests/link.test.tsx @@ -3030,6 +3030,54 @@ describe('Link', () => { expect(ErrorComponent).not.toHaveBeenCalled() }) + + test('when linking to self with from prop set and param containing a slash', async () => { + const ErrorComponent = vi.fn(() =>

Something went wrong!

) + + const rootRoute = createRootRoute({ + errorComponent: ErrorComponent, + }) + + const indexRoute = createRoute({ + getParentRoute: () => rootRoute, + path: '/', + component: () => ( + + Go to post + + ), + }) + + const postRoute = createRoute({ + getParentRoute: () => rootRoute, + path: '/$postId', + component: () => ( + + Link to self with from prop set + + ), + }) + + const routeTree = rootRoute.addChildren([indexRoute, postRoute]) + const router = createRouter({ routeTree }) + + render() + + const postLink = await screen.findByRole('link', { + name: 'Go to post', + }) + + expect(postLink).toHaveAttribute('href', '/id%2Fwith-slash') + + fireEvent.click(postLink) + + const selfLink = await screen.findByRole('link', { + name: 'Link to self with from prop set', + }) + + expect(selfLink).toBeInTheDocument() + expect(ErrorComponent).not.toHaveBeenCalled() + }) }) describe('createLink', () => {