Unverified Commit d0bf3ee6 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Code refactoring (#35937)

Clean up some code in the renderer and make it more readable.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
parent cb565d69
...@@ -305,28 +305,14 @@ export async function renderToStream({ ...@@ -305,28 +305,14 @@ export async function renderToStream({
generateStaticHTML: boolean generateStaticHTML: boolean
flushEffectHandler?: () => Promise<string> flushEffectHandler?: () => Promise<string>
}): Promise<ReadableStream<Uint8Array>> { }): Promise<ReadableStream<Uint8Array>> {
const closeTag = '</body></html>' const renderStream = await renderToInitialStream({ ReactDOMServer, element })
const suffixUnclosed = suffix ? suffix.split(closeTag)[0] : null return continueFromInitialStream({
const renderStream: ReadableStream<Uint8Array> & { suffix,
allReady?: Promise<void> dataStream,
} = await (ReactDOMServer as any).renderToReadableStream(element) generateStaticHTML,
flushEffectHandler,
if (generateStaticHTML) { renderStream,
await renderStream.allReady })
}
const transforms: Array<TransformStream<Uint8Array, Uint8Array>> = [
createBufferedTransformStream(),
flushEffectHandler ? createFlushEffectStream(flushEffectHandler) : null,
suffixUnclosed != null ? createPrefixStream(suffixUnclosed) : null,
dataStream ? createInlineDataStream(dataStream) : null,
suffixUnclosed != null ? createSuffixStream(closeTag) : null,
].filter(Boolean) as any
return transforms.reduce(
(readable, transform) => pipeThrough(readable, transform),
renderStream
)
} }
export function createSuffixStream( export function createSuffixStream(
......
...@@ -1424,19 +1424,15 @@ export async function renderToHTML( ...@@ -1424,19 +1424,15 @@ export async function renderToHTML(
} }
} }
} else { } else {
let bodyResult
let renderStream: any
// We start rendering the shell earlier, before returning the head tags // We start rendering the shell earlier, before returning the head tags
// to `documentResult`. // to `documentResult`.
const content = renderContent() const content = renderContent()
renderStream = await renderToInitialStream({ const renderStream = await renderToInitialStream({
ReactDOMServer, ReactDOMServer,
element: content, element: content,
}) })
bodyResult = async (suffix: string) => { const bodyResult = async (suffix: string) => {
// this must be called inside bodyResult so appWrappers is // this must be called inside bodyResult so appWrappers is
// up to date when getWrappedApp is called // up to date when getWrappedApp is called
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment