Skip to content

Commit e92d5b7

Browse files
authored
fix: stream ssr concat buffer first (#7830)
1 parent f73c541 commit e92d5b7

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

.changeset/soft-plums-go.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: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export const createReadableStreamFromElement: CreateReadableStreamFromElement =
3434

3535
const sheet = new ServerStyleSheet();
3636

37-
const chunkVec: string[] = [];
37+
const chunkVec: Buffer[] = [];
3838

3939
const root = sheet.collectStyles(rootElement);
4040

@@ -62,15 +62,19 @@ export const createReadableStreamFromElement: CreateReadableStreamFromElement =
6262
transform(chunk, _encoding, callback) {
6363
try {
6464
if (shellChunkStatus !== ShellChunkStatus.FINISH) {
65-
chunkVec.push(chunk.toString());
65+
chunkVec.push(
66+
Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk),
67+
);
6668
/**
6769
* The shell content of App may be splitted by multiple chunks to transform,
6870
* when any node value's size is larger than the React limitation, refer to:
6971
* https://github.com/facebook/react/blob/v18.2.0/packages/react-server/src/ReactServerStreamConfigNode.js#L53.
7072
* So we use the `SHELL_STREAM_END_MARK` to mark the shell content' tail.
7173
*/
72-
let concatedChunk = chunkVec.join('');
73-
if (concatedChunk.includes(ESCAPED_SHELL_STREAM_END_MARK)) {
74+
const chunkStr = chunk.toString('utf-8');
75+
if (chunkStr.includes(ESCAPED_SHELL_STREAM_END_MARK)) {
76+
let concatedChunk =
77+
Buffer.concat(chunkVec).toString('utf-8');
7478
concatedChunk = concatedChunk.replace(
7579
ESCAPED_SHELL_STREAM_END_MARK,
7680
'',

0 commit comments

Comments
 (0)