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
5 changes: 5 additions & 0 deletions .changeset/blue-parrots-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-router": patch
---

Fix double slash normalization for useNavigate colon urls
42 changes: 26 additions & 16 deletions packages/react-router/__tests__/resolvePath-test.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,6 @@
import { resolvePath } from "react-router";

describe("resolvePath", () => {
it("does not touch with protocol-less absolute paths", () => {
expect(resolvePath("//google.com")).toMatchObject({
pathname: "//google.com",
});

expect(resolvePath("//google.com/../../path")).toMatchObject({
pathname: "//google.com/../../path",
});

expect(resolvePath("//google.com?q=query#hash")).toMatchObject({
pathname: "//google.com",
search: "?q=query",
hash: "#hash",
});
});

it('resolves absolute paths irrespective of the "from" pathname', () => {
expect(resolvePath("/search", "/inbox")).toMatchObject({
pathname: "/search",
Expand Down Expand Up @@ -79,6 +63,32 @@ describe("resolvePath", () => {
spy.mockRestore();
});

it("handles relative paths with an embedded colon", () => {
expect(resolvePath("foo:bar", "/")).toMatchObject({
pathname: "/foo:bar",
});

expect(resolvePath("./foo:bar", "/")).toMatchObject({
pathname: "/foo:bar",
});

expect(resolvePath("../foo:bar", "/")).toMatchObject({
pathname: "/foo:bar",
});

expect(resolvePath("foo:bar", "/path")).toMatchObject({
pathname: "/path/foo:bar",
});

expect(resolvePath("./foo:bar", "/path")).toMatchObject({
pathname: "/path/foo:bar",
});

expect(resolvePath("../foo:bar", "/path")).toMatchObject({
pathname: "/foo:bar",
});
});

it('ignores trailing slashes on the "from" pathname when resolving relative paths', () => {
expect(resolvePath("../search", "/inbox/")).toMatchObject({
pathname: "/search",
Expand Down
20 changes: 4 additions & 16 deletions packages/react-router/lib/router/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1603,23 +1603,11 @@ export function resolvePath(to: To, fromPathname = "/"): Path {

let pathname: string;
if (toPathname) {
if (isAbsoluteUrl(toPathname)) {
pathname = toPathname;
toPathname = toPathname.replace(/\/\/+/g, "/");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We didn't need to be checking for absolute URLs at all here before. resolvePath is only intended to manage pathnames/search/hash - absolute navigations are handled outside of here by Link so we can just always do the double slash replacement here.

if (toPathname.startsWith("/")) {
pathname = resolvePathname(toPathname.substring(1), "/");
} else {
if (toPathname.includes("//")) {
let oldPathname = toPathname;
toPathname = toPathname.replace(/\/\/+/g, "/");
warning(
false,
`Pathnames cannot have embedded double slashes - normalizing ` +
`${oldPathname} -> ${toPathname}`,
);
}
if (toPathname.startsWith("/")) {
pathname = resolvePathname(toPathname.substring(1), "/");
} else {
pathname = resolvePathname(toPathname, fromPathname);
}
pathname = resolvePathname(toPathname, fromPathname);
}
} else {
pathname = fromPathname;
Expand Down