Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/tame-sloths-look.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@modern-js/runtime': patch
---

fix(plugin-runtime): streaming ssr split chunk so that can't match `SHELL_STREAM_END_MARK`
fix(plugin-runtime): streaming ssr chunk 进行分割导致无法匹配 `SHELL_STREAM_END_MARK`
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ const DeferredDataScript = ({
<ErrorDeferredDataScript routeId={routeId} dataKey={dataKey} />
}
>
{data => (
{(data: any) => (
<script
async
suppressHydrationWarning
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ import { getTemplates } from './template';
export type Pipe<T extends Writable> = (output: T) => Promise<T | string>;

enum ShellChunkStatus {
IDLE = 0,
START = 1,
FINIESH = 2,
START = 0,
FINIESH = 1,
}

function renderToPipe(
rootElement: React.ReactElement,
context: RuntimeContext,
options?: RenderToPipeableStreamOptions,
) {
let shellChunkStatus = ShellChunkStatus.IDLE;
let shellChunkStatus = ShellChunkStatus.START;

const { ssrContext } = context;
const chunkVec: string[] = [];
const forUserPipe: Pipe<Writable> = stream => {
return new Promise(resolve => {
let renderToPipeableStream;
Expand All @@ -40,28 +40,24 @@ function renderToPipe(
transform(chunk, _encoding, callback) {
try {
if (shellChunkStatus !== ShellChunkStatus.FINIESH) {
let concatedChunk = chunk.toString();
if (shellChunkStatus === ShellChunkStatus.IDLE) {
concatedChunk = `${shellBefore}${concatedChunk}`;
shellChunkStatus = ShellChunkStatus.START;
}
chunkVec.push(chunk.toString());

/**
* The shell content of App may be splitted by multiple chunks to transform,
* when any node value's size is larger than the React limitation, refer to:
* https://github.com/facebook/react/blob/v18.2.0/packages/react-server/src/ReactServerStreamConfigNode.js#L53.
* So we use the `SHELL_STREAM_END_MARK` to mark the shell content' tail.
*/
if (
shellChunkStatus === ShellChunkStatus.START &&
concatedChunk.endsWith(ESCAPED_SHELL_STREAM_END_MARK)
) {
let concatedChunk = chunkVec.join('');
if (concatedChunk.endsWith(ESCAPED_SHELL_STREAM_END_MARK)) {
concatedChunk = concatedChunk.replace(
ESCAPED_SHELL_STREAM_END_MARK,
shellAfter,
'',
);

shellChunkStatus = ShellChunkStatus.FINIESH;
this.push(`${shellBefore}${concatedChunk}${shellAfter}`);
}
this.push(concatedChunk);
} else {
this.push(chunk);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ import { getTemplates } from './template';
export type Pipe<T extends Writable> = (output: T) => Promise<T | string>;

enum ShellChunkStatus {
IDLE = 0,
START = 1,
FINIESH = 2,
START = 0,
FINIESH = 1,
}

function renderToPipe(
rootElement: React.ReactElement,
context: RuntimeContext,
options?: RenderToReadableStreamOptions,
) {
let shellChunkStatus = ShellChunkStatus.IDLE;
let shellChunkStatus = ShellChunkStatus.START;
const chunkVec: string[] = [];

const { ssrContext } = context;
const forUserPipe = async () => {
Expand Down Expand Up @@ -56,20 +56,24 @@ function renderToPipe(
return;
}
if (shellChunkStatus !== ShellChunkStatus.FINIESH) {
let concatedChunk = new TextDecoder().decode(value);
if (shellChunkStatus === ShellChunkStatus.IDLE) {
concatedChunk = `${shellBefore}${concatedChunk}`;
shellChunkStatus = ShellChunkStatus.START;
}
if (
shellChunkStatus === ShellChunkStatus.START &&
concatedChunk.endsWith(ESCAPED_SHELL_STREAM_END_MARK)
) {
const chunk = new TextDecoder().decode(value);

chunkVec.push(chunk);

let concatedChunk = chunkVec.join('');
if (concatedChunk.endsWith(ESCAPED_SHELL_STREAM_END_MARK)) {
concatedChunk = concatedChunk.replace(
ESCAPED_SHELL_STREAM_END_MARK,
shellAfter!,
'',
);

shellChunkStatus = ShellChunkStatus.FINIESH;

controller.enqueue(
encodeForWebStream(
`${shellBefore}${concatedChunk}${shellAfter}`,
),
);
}
controller.enqueue(encodeForWebStream(concatedChunk));
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type InjectTemplate = {
shellBefore?: string;
shellAfter?: string;
shellBefore: string;
shellAfter: string;
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ const Home = lazy(() => import('./Home'));

const App: React.FunctionComponent = () => {
return (
<div>
<div>App Layout</div>
<Suspense fallback={<div>loading home...</div>}>
<Home />
</Suspense>
</div>
<>
<div>
<div>App Layout</div>
<Suspense fallback={<div>loading home...</div>}>
<Home />
</Suspense>
</div>
{'<!--<?- SHELL_STREAM_END ?>-->'}
</>
);
};

Expand Down