Skip to content

Instantly share code, notes, and snippets.

@khg0712
Last active May 28, 2023 18:13
Show Gist options
  • Save khg0712/ad7f00b4f5b02070bc4dc316ce546333 to your computer and use it in GitHub Desktop.
Save khg0712/ad7f00b4f5b02070bc4dc316ce546333 to your computer and use it in GitHub Desktop.
// Next.js v12.3.4 버전 기준 SSR 코드
// packages/next/server/render.tsx
// https://github.com/vercel/next.js/blob/v12.3.4/packages/next/server/render.tsx#L1114
const renderDocument = async () => {
// ...
//
const createBodyResult = (
initialStream: ReactReadableStream,
suffix?: string
) => {
// ReactDOMServer.renderToString 호출
const flushEffectHandler = (): string => {
const flushed = ReactDOMServer.renderToString(styledJsxFlushEffect())
return flushed
}
// flushEffectHandler를 callback으로 넣어서
// body render하는 stream 생성
return continueFromInitialStream(initialStream, {
// ...
flushEffectHandler,
})
}
// createBodyResult로 body render 결과 생성
if (hasDocumentGetInitialProps) {
// ...
bodyResult = (suffix: string) =>
createBodyResult(streamFromArray([docProps.html, suffix]))
} else {
// ...
bodyResult = (suffix: string) => createBodyResult(stream, suffix)
// ...
}
// ...
// bodyResult가 포함된 render 결과 리턴
return {
bodyResult,
// ...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment