diff --git a/contributors.yml b/contributors.yml index 2082053191..17f93e5e17 100644 --- a/contributors.yml +++ b/contributors.yml @@ -281,3 +281,4 @@ - yuleicul - zeromask1337 - zheng-chuang +- levvysokiy diff --git a/packages/react-router/__tests__/generatePath-test.tsx b/packages/react-router/__tests__/generatePath-test.tsx index 70232f7d46..583b2ff065 100644 --- a/packages/react-router/__tests__/generatePath-test.tsx +++ b/packages/react-router/__tests__/generatePath-test.tsx @@ -58,6 +58,11 @@ describe("generatePath", () => { "/courses/baz" ); }); + it("handles slashes in dynamic params", () => { + expect(generatePath("/courses/:id", { id: "foo/bar" })).toBe( + "/courses/foo%2Fbar" + ); + }); }); describe("with extraneous params", () => { diff --git a/packages/react-router/lib/router/utils.ts b/packages/react-router/lib/router/utils.ts index f6396d2e5d..6be35b1768 100644 --- a/packages/react-router/lib/router/utils.ts +++ b/packages/react-router/lib/router/utils.ts @@ -874,6 +874,9 @@ export function generatePath( const [, key, optional] = keyMatch; let param = params[key as PathParam]; invariant(optional === "?" || param != null, `Missing ":${key}" param`); + if (typeof param === "string") { + param = param.replace(/\//g, "%2F"); + } return stringify(param); }