Skip to content

Commit 61daf31

Browse files
committed
fix _root.data fetch when basename is set
1 parent 1e6b2e1 commit 61daf31

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

contributors.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- akamfoad
1717
- alany411
1818
- alberto
19+
- Aleuck
1920
- alexandernanberg
2021
- alexanderson1993
2122
- alexlbr
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { singleFetchUrl } from "../../../lib/dom/ssr/single-fetch";
2+
3+
describe("singleFetchUrl", () => {
4+
it("should resolve to _root.data when path is /", () => {
5+
window.__reactRouterContext = {} as any;
6+
const actual = singleFetchUrl(`${window.location.origin}/`);
7+
expect(actual.toString()).toBe(`${window.location.origin}/_root.data`);
8+
});
9+
10+
it("should resolve to _root.data when path is equal to basename", () => {
11+
window.__reactRouterContext = {} as any;
12+
window.__reactRouterContext!.basename = "/basename/";
13+
const actual = singleFetchUrl(`${window.location.origin}/basename/`);
14+
expect(actual.toString()).toBe(
15+
`${window.location.origin}/basename/_root.data`
16+
);
17+
});
18+
19+
it("should resolve to path.data when path is not to base", () => {
20+
window.__reactRouterContext = {} as any;
21+
const actual = singleFetchUrl(`${window.location.origin}/path/`);
22+
expect(actual.toString()).toBe(`${window.location.origin}/path.data`);
23+
});
24+
});

packages/react-router/lib/dom/ssr/single-fetch.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,10 @@ function stripIndexParam(url: URL) {
444444
}
445445

446446
export function singleFetchUrl(reqUrl: URL | string) {
447+
let basename =
448+
(typeof window !== "undefined" && window.__reactRouterContext?.basename) ||
449+
"";
450+
basename = `/${basename}/`.replace(/\/+/g, "/");
447451
let url =
448452
typeof reqUrl === "string"
449453
? new URL(
@@ -456,8 +460,8 @@ export function singleFetchUrl(reqUrl: URL | string) {
456460
)
457461
: reqUrl;
458462

459-
if (url.pathname === "/") {
460-
url.pathname = "_root.data";
463+
if (url.pathname === basename) {
464+
url.pathname = `${basename}_root.data`;
461465
} else {
462466
url.pathname = `${url.pathname.replace(/\/$/, "")}.data`;
463467
}

packages/react-router/lib/server-runtime/server.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,10 @@ export const createRequestHandler: CreateRequestHandlerFunction = (
107107
}
108108

109109
let url = new URL(request.url);
110+
let basebane = `/${_build.basename || ''}/`.replace(/\/+/g,'/')
110111
let normalizedPath = url.pathname
111112
.replace(/\.data$/, "")
112-
.replace(/^\/_root$/, "/");
113+
.replace(new RegExp(`^${basebane}_root$`), basebane);
113114
if (normalizedPath !== "/" && normalizedPath.endsWith("/")) {
114115
normalizedPath = normalizedPath.slice(0, -1);
115116
}

0 commit comments

Comments
 (0)