Skip to content

Commit

Permalink
Code refactoring (vercel#35937)
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
shuding authored and colinhacks committed Apr 14, 2022
1 parent ba72838 commit bee4ede
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 28 deletions.
30 changes: 8 additions & 22 deletions packages/next/server/node-web-streams-helper.ts
Expand Up @@ -305,28 +305,14 @@ export async function renderToStream({
generateStaticHTML: boolean
flushEffectHandler?: () => Promise<string>
}): Promise<ReadableStream<Uint8Array>> {
const closeTag = '</body></html>'
const suffixUnclosed = suffix ? suffix.split(closeTag)[0] : null
const renderStream: ReadableStream<Uint8Array> & {
allReady?: Promise<void>
} = await (ReactDOMServer as any).renderToReadableStream(element)

if (generateStaticHTML) {
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
)
const renderStream = await renderToInitialStream({ ReactDOMServer, element })
return continueFromInitialStream({
suffix,
dataStream,
generateStaticHTML,
flushEffectHandler,
renderStream,
})
}

export function createSuffixStream(
Expand Down
8 changes: 2 additions & 6 deletions packages/next/server/render.tsx
Expand Up @@ -1424,19 +1424,15 @@ export async function renderToHTML(
}
}
} else {
let bodyResult

let renderStream: any

// We start rendering the shell earlier, before returning the head tags
// to `documentResult`.
const content = renderContent()
renderStream = await renderToInitialStream({
const renderStream = await renderToInitialStream({
ReactDOMServer,
element: content,
})

bodyResult = async (suffix: string) => {
const bodyResult = async (suffix: string) => {
// this must be called inside bodyResult so appWrappers is
// up to date when getWrappedApp is called

Expand Down

0 comments on commit bee4ede

Please sign in to comment.