Skip to content

Commit 7cbd9e1

Browse files
authored
fix: stream ssr should concat buffer first (#7836)
1 parent c161b5c commit 7cbd9e1

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

.changeset/grumpy-regions-join.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@modern-js/runtime': patch
3+
---
4+
5+
fix: stream ssr should concat buffer first, then stringify buffer
6+
fix: stream ssr 先拼接 buffer, 再将 buffer 处理成字符串

packages/runtime/plugin-runtime/src/core/server/stream/createReadableStream.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export const createReadableStreamFromElement: CreateReadableStreamFromElement =
6363
}
6464
});
6565

66-
const chunkVec: string[] = [];
66+
const chunkVec: Buffer[] = [];
6767

6868
return new Promise(resolve => {
6969
const { pipe: reactStreamingPipe } = renderToPipeableStream(
@@ -94,17 +94,19 @@ export const createReadableStreamFromElement: CreateReadableStreamFromElement =
9494
transform(chunk, _encoding, callback) {
9595
try {
9696
if (shellChunkStatus !== ShellChunkStatus.FINISH) {
97-
chunkVec.push(chunk.toString());
98-
/**
97+
chunkVec.push(
98+
Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk),
99+
); /**
99100
* The shell content of App may be splitted by multiple chunks to transform,
100101
* when any node value's size is larger than the React limitation, refer to:
101102
* https://github.com/facebook/react/blob/v18.2.0/packages/react-server/src/ReactServerStreamConfigNode.js#L53.
102103
* So we use the `SHELL_STREAM_END_MARK` to mark the shell content' tail.
103104
*/
104-
let concatedChunk = chunkVec.join('');
105-
if (
106-
concatedChunk.includes(ESCAPED_SHELL_STREAM_END_MARK)
107-
) {
105+
const chunkStr = chunk.toString('utf-8');
106+
if (chunkStr.includes(ESCAPED_SHELL_STREAM_END_MARK)) {
107+
let concatedChunk = Buffer.concat(
108+
chunkVec as any,
109+
).toString('utf-8');
108110
concatedChunk = concatedChunk.replace(
109111
ESCAPED_SHELL_STREAM_END_MARK,
110112
'',

0 commit comments

Comments
 (0)