Skip to content
Open
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
9 changes: 9 additions & 0 deletions .changeset/kind-coins-serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"react-router": patch
---

Fix RSC double slashes in manifest URLs

Normalize double slashes in getManifestUrl function to prevent ERR_NAME_NOT_RESOLVED errors when URLs contain double slashes like //en//test2/test


1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -459,3 +459,4 @@
- zeromask1337
- zheng-chuang
- zxTomw
- swarnim02
20 changes: 20 additions & 0 deletions packages/react-router/__tests__/rsc-double-slashes-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { joinPaths } from "../lib/router/utils";

describe("joinPaths double slash normalization", () => {
it("normalizes double slashes in single path", () => {
expect(joinPaths(["//en//test2/test"])).toBe("/en/test2/test");
expect(joinPaths(["/app//base/"])).toBe("/app/base/");
expect(joinPaths(["///multiple///slashes"])).toBe("/multiple/slashes");
});

it("normalizes double slashes in multiple paths", () => {
expect(joinPaths(["//en//test1", "//fr//test2"])).toBe("/en/test1/fr/test2");
expect(joinPaths(["path//with//double", "slashes//here"])).toBe("path/with/double/slashes/here");
});

it("preserves normal paths", () => {
expect(joinPaths(["/normal/path"])).toBe("/normal/path");
expect(joinPaths(["path/without/leading/slash"])).toBe("path/without/leading/slash");
expect(joinPaths([""])).toBe("");
});
});
8 changes: 6 additions & 2 deletions packages/react-router/lib/router/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1777,8 +1777,12 @@ export function resolveTo(
return path;
}

export const joinPaths = (paths: string[]): string =>
paths.join("/").replace(/\/\/+/g, "/");
export const joinPaths = (paths: string[]): string => {
return paths
.map(path => path.replace(/\/\/+/g, "/")) // Normalize double slashes within each path
.join("/")
.replace(/\/\/+/g, "/"); // Normalize any double slashes created by joining
};

export const normalizePathname = (pathname: string): string =>
pathname.replace(/\/+$/, "").replace(/^\/*/, "/");
Expand Down
12 changes: 9 additions & 3 deletions packages/react-router/lib/rsc/browser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import type {
DataStrategyFunctionArgs,
RouterContextProvider,
} from "../router/utils";
import { ErrorResponseImpl, createContext } from "../router/utils";
import { ErrorResponseImpl, createContext, joinPaths } from "../router/utils";
import type {
DecodedSingleFetchResults,
FetchAndDecodeFunction,
Expand Down Expand Up @@ -972,16 +972,22 @@ function getManifestUrl(paths: string[]): URL | null {
}

if (paths.length === 1) {
return new URL(`${paths[0]}.manifest`, window.location.origin);
// Normalize double slashes in the single path
const normalizedPath = joinPaths([paths[0]]);
return new URL(`${normalizedPath}.manifest`, window.location.origin);
}

const globalVar = window as WindowWithRouterGlobals;
let basename = (globalVar.__reactRouterDataRouter.basename ?? "").replace(
/^\/|\/$/g,
"",
);
// Normalize double slashes in basename
basename = joinPaths([basename]);
let url = new URL(`${basename}/.manifest`, window.location.origin);
url.searchParams.set("paths", paths.sort().join(","));
// Normalize double slashes in all paths before joining
const normalizedPaths = paths.map(path => joinPaths([path]));
url.searchParams.set("paths", normalizedPaths.sort().join(","));

return url;
}
Expand Down