Skip to content

Commit b3b90aa

Browse files
committed
fix basename tests migrate existing path normalization logic
1 parent a08a14d commit b3b90aa

File tree

2 files changed

+39
-21
lines changed

2 files changed

+39
-21
lines changed

integration/vite-basename-test.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ const customServerFile = ({
122122
app.use("${base}", express.static("build/client"));
123123
app.all(
124124
"${basename}*",
125-
createRequestListener((await import("./build/server/index.js")).default),
125+
createRequestListener((await import("./build/server/index.js")).default.fetch),
126126
);
127127
}
128128
app.get("*", (_req, res) => {
@@ -476,7 +476,7 @@ test.describe("Vite base + React Router basename", () => {
476476
}
477477
}
478478

479-
test.afterAll(() => stop());
479+
test.afterAll(() => stop?.());
480480

481481
test("works when base and basename are the same", async ({ page }) => {
482482
await setup({ base: "/mybase/", basename: "/mybase/" });
@@ -504,6 +504,10 @@ test.describe("Vite base + React Router basename", () => {
504504
test("works when when base is an absolute external URL", async ({
505505
page,
506506
}) => {
507+
test.skip(
508+
templateName === "rsc-vite-framework",
509+
"I'm not sure the use-case for this and can't find anything. If you ever need this file an issue and we will revisit.",
510+
);
507511
port = await getPort();
508512
cwd = await createProject(
509513
{
@@ -518,26 +522,26 @@ test.describe("Vite base + React Router basename", () => {
518522
? String.raw`
519523
import { createRequestListener } from "@mjackson/node-fetch-server";
520524
import express from "express";
521-
525+
522526
const app = express();
523527
app.all(
524528
"/app/*",
525529
createRequestListener((await import("./build/server/index.js")).default)
526530
);
527-
531+
528532
const port = ${port};
529533
app.listen(port, () => console.log('http://localhost:' + port));
530534
`
531535
: String.raw`
532536
import { createRequestHandler } from "@react-router/express";
533537
import express from "express";
534-
538+
535539
const app = express();
536540
app.all(
537541
"/app/*",
538542
createRequestHandler({ build: await import("./build/server/index.js") })
539543
);
540-
544+
541545
const port = ${port};
542546
app.listen(port, () => console.log('http://localhost:' + port));
543547
`,

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

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -394,10 +394,37 @@ export async function matchRSCServerRequest({
394394
},
395395
) => Response;
396396
}): Promise<Response> {
397-
let requestUrl = new URL(request.url);
397+
let url = new URL(request.url);
398+
399+
basename = basename || "/";
400+
let normalizedPath = url.pathname;
401+
if (stripBasename(normalizedPath, basename) === "/_root.rsc") {
402+
normalizedPath = basename;
403+
} else if (normalizedPath.endsWith(".rsc")) {
404+
normalizedPath = normalizedPath.replace(/\.rsc$/, "");
405+
}
406+
407+
if (
408+
stripBasename(normalizedPath, basename) !== "/" &&
409+
normalizedPath.endsWith("/")
410+
) {
411+
normalizedPath = normalizedPath.slice(0, -1);
412+
}
413+
url.pathname = normalizedPath;
414+
basename =
415+
basename.length > normalizedPath.length ? normalizedPath : basename;
416+
417+
let routerRequest = new Request(url.toString(), {
418+
method: request.method,
419+
headers: request.headers,
420+
body: request.body,
421+
signal: request.signal,
422+
duplex: request.body ? "half" : undefined,
423+
} as RequestInit);
398424

399425
const temporaryReferences = createTemporaryReferenceSet();
400426

427+
const requestUrl = new URL(request.url);
401428
if (isManifestRequest(requestUrl)) {
402429
let response = await generateManifestResponse(
403430
routes,
@@ -411,19 +438,6 @@ export async function matchRSCServerRequest({
411438

412439
let isDataRequest = isReactServerRequest(requestUrl);
413440

414-
const url = new URL(request.url);
415-
let routerRequest = request;
416-
if (isDataRequest) {
417-
url.pathname = url.pathname.replace(/(_root)?\.rsc$/, "");
418-
routerRequest = new Request(url.toString(), {
419-
method: request.method,
420-
headers: request.headers,
421-
body: request.body,
422-
signal: request.signal,
423-
duplex: request.body ? "half" : undefined,
424-
} as RequestInit);
425-
}
426-
427441
// Explode lazy functions out the routes so we can use middleware
428442
// TODO: This isn't ideal but we can't do it through `lazy()` in the router,
429443
// and if we move to `lazy: {}` then we lose all the other things from the
@@ -1009,7 +1023,7 @@ async function generateStaticContextResponse(
10091023

10101024
const baseRenderPayload: Omit<RSCRenderPayload, "matches" | "patches"> = {
10111025
type: "render",
1012-
basename,
1026+
basename: staticContext.basename,
10131027
actionData: staticContext.actionData,
10141028
errors: staticContext.errors,
10151029
loaderData: staticContext.loaderData,

0 commit comments

Comments
 (0)