From 6226a4b57e0a2f92219aff980b5d3de092885f96 Mon Sep 17 00:00:00 2001 From: Jakob Norlin Date: Wed, 19 Jun 2024 09:05:00 +0200 Subject: [PATCH 1/4] Add test case for linking with from prop and a param containing slash --- packages/react-router/tests/link.test.tsx | 41 +++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/packages/react-router/tests/link.test.tsx b/packages/react-router/tests/link.test.tsx index 74f4468e2b..975f128aa6 100644 --- a/packages/react-router/tests/link.test.tsx +++ b/packages/react-router/tests/link.test.tsx @@ -3030,6 +3030,47 @@ describe('Link', () => { expect(ErrorComponent).not.toHaveBeenCalled() }) + + test.only('when linking to self with from prop set and param containing a slash', async () => { + const rootRoute = createRootRoute({}) + + 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', + }) + + fireEvent.click(postLink) + + const selfLink = await screen.findByRole('link', { + name: 'Link to self with from prop set', + }) + + expect(selfLink).toBeInTheDocument() + }) }) describe('createLink', () => { From f83b45538db448272f514107e2e66c710c979b51 Mon Sep 17 00:00:00 2001 From: Jakob Norlin Date: Wed, 19 Jun 2024 23:54:23 +0200 Subject: [PATCH 2/4] Remove .only from test --- packages/react-router/tests/link.test.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/react-router/tests/link.test.tsx b/packages/react-router/tests/link.test.tsx index 975f128aa6..cae558ec9a 100644 --- a/packages/react-router/tests/link.test.tsx +++ b/packages/react-router/tests/link.test.tsx @@ -3031,7 +3031,7 @@ describe('Link', () => { expect(ErrorComponent).not.toHaveBeenCalled() }) - test.only('when linking to self with from prop set and param containing a slash', async () => { + test('when linking to self with from prop set and param containing a slash', async () => { const rootRoute = createRootRoute({}) const indexRoute = createRoute({ From 0bfc57603ea75ccbd308dcdb6f96d374e87baef0 Mon Sep 17 00:00:00 2001 From: chorobin Date: Thu, 20 Jun 2024 00:07:38 +0200 Subject: [PATCH 3/4] fix: encode pathname in match --- packages/react-router/src/router.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/react-router/src/router.ts b/packages/react-router/src/router.ts index 9523919b7a..5f940087c5 100644 --- a/packages/react-router/src/router.ts +++ b/packages/react-router/src/router.ts @@ -982,9 +982,20 @@ export class Router< const loaderDepsHash = loaderDeps ? JSON.stringify(loaderDeps) : '' + const encodedParams: Record = {} + + for (const [key, value] of Object.entries(routeParams)) { + if (['*', '_splat'].includes(key)) { + // the splat/catch-all routes shouldn't have the '/' encoded out + encodedParams[key] = encodeURI(value) + } else { + encodedParams[key] = encodeURIComponent(value) + } + } + const interpolatedPath = interpolatePath({ path: route.fullPath, - params: routeParams, + params: encodedParams, }) const matchId = From 881390755c74b3ddb27696fe3ad7ab11bd5c927b Mon Sep 17 00:00:00 2001 From: chorobin Date: Thu, 20 Jun 2024 23:28:26 +0200 Subject: [PATCH 4/4] refactor: move encoded params to interpolatePath --- packages/react-router/src/path.ts | 18 +++++++++++++++--- packages/react-router/src/router.ts | 23 +---------------------- packages/react-router/tests/link.test.tsx | 9 ++++++++- 3 files changed, 24 insertions(+), 26 deletions(-) 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 5f940087c5..021d968404 100644 --- a/packages/react-router/src/router.ts +++ b/packages/react-router/src/router.ts @@ -982,20 +982,9 @@ export class Router< const loaderDepsHash = loaderDeps ? JSON.stringify(loaderDeps) : '' - const encodedParams: Record = {} - - for (const [key, value] of Object.entries(routeParams)) { - if (['*', '_splat'].includes(key)) { - // the splat/catch-all routes shouldn't have the '/' encoded out - encodedParams[key] = encodeURI(value) - } else { - encodedParams[key] = encodeURIComponent(value) - } - } - const interpolatedPath = interpolatePath({ path: route.fullPath, - params: encodedParams, + params: routeParams, }) const matchId = @@ -1181,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 cae558ec9a..e6fa2f5229 100644 --- a/packages/react-router/tests/link.test.tsx +++ b/packages/react-router/tests/link.test.tsx @@ -3032,7 +3032,11 @@ describe('Link', () => { }) test('when linking to self with from prop set and param containing a slash', async () => { - const rootRoute = createRootRoute({}) + const ErrorComponent = vi.fn(() =>

Something went wrong!

) + + const rootRoute = createRootRoute({ + errorComponent: ErrorComponent, + }) const indexRoute = createRoute({ getParentRoute: () => rootRoute, @@ -3063,6 +3067,8 @@ describe('Link', () => { name: 'Go to post', }) + expect(postLink).toHaveAttribute('href', '/id%2Fwith-slash') + fireEvent.click(postLink) const selfLink = await screen.findByRole('link', { @@ -3070,6 +3076,7 @@ describe('Link', () => { }) expect(selfLink).toBeInTheDocument() + expect(ErrorComponent).not.toHaveBeenCalled() }) })