Skip to content

Commit cf78891

Browse files
committed
fix: decode each time getPayload() is called to allow for "in-context" decoding and hoisting of contextual assets
chore: use `vite build` for rsc framework fixtures
1 parent ab55864 commit cf78891

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

.changeset/shiny-hotels-guess.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"react-router": patch
3+
---
4+
5+
decode each time `getPayload()` is called to allow for "in-context" decoding and hoisting of contextual assets

integration/helpers/create-fixture.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,12 @@ export async function createFixtureProject(
480480
if (templateName.includes("parcel")) {
481481
parcelBuild(projectDir, init.buildStdio, mode);
482482
} else {
483-
reactRouterBuild(projectDir, init.buildStdio, mode);
483+
reactRouterBuild(
484+
projectDir,
485+
init.buildStdio,
486+
mode,
487+
templateName.includes("rsc"),
488+
);
484489
}
485490

486491
return projectDir;
@@ -526,6 +531,7 @@ function reactRouterBuild(
526531
projectDir: string,
527532
buildStdio?: Writable,
528533
mode?: ServerMode,
534+
isRsc?: boolean,
529535
) {
530536
// We have a "require" instead of a dynamic import in readConfig gated
531537
// behind mode === ServerMode.Test to make jest happy, but that doesn't
@@ -535,8 +541,9 @@ function reactRouterBuild(
535541
mode = mode === ServerMode.Test ? ServerMode.Production : mode;
536542

537543
let reactRouterBin = "node_modules/@react-router/dev/dist/cli/index.js";
544+
let viteBin = "node_modules/vite/dist/node/cli.js";
538545

539-
let buildArgs: string[] = [reactRouterBin, "build"];
546+
let buildArgs: string[] = [isRsc ? viteBin : reactRouterBin, "build"];
540547

541548
let buildSpawn = spawnSync("node", buildArgs, {
542549
cwd: projectDir,

packages/react-router/lib/rsc/server.ssr.tsx

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,38 @@ export async function routeRSCServerRequest({
120120
}
121121

122122
const body = serverResponse.body;
123-
let payloadPromise: Promise<RSCPayload>;
123+
124+
let buffer: Uint8Array[] | undefined;
125+
let streamControllers: ReadableStreamDefaultController<Uint8Array>[] = [];
126+
127+
const createStream = () => {
128+
if (!buffer) {
129+
buffer = [];
130+
return body.pipeThrough(
131+
new TransformStream({
132+
transform(chunk, controller) {
133+
buffer!.push(chunk);
134+
controller.enqueue(chunk);
135+
streamControllers.forEach((c) => c.enqueue(chunk));
136+
},
137+
flush() {
138+
streamControllers.forEach((c) => c.close());
139+
streamControllers = [];
140+
},
141+
}),
142+
);
143+
}
144+
145+
return new ReadableStream({
146+
start(controller) {
147+
buffer!.forEach((chunk) => controller.enqueue(chunk));
148+
streamControllers.push(controller);
149+
},
150+
});
151+
};
152+
124153
const getPayload = async () => {
125-
if (payloadPromise) return payloadPromise;
126-
payloadPromise = createFromReadableStream(body) as Promise<RSCPayload>;
127-
return payloadPromise;
154+
return createFromReadableStream(createStream()) as Promise<RSCPayload>;
128155
};
129156

130157
try {

0 commit comments

Comments
 (0)