Last active
May 28, 2023 18:13
-
-
Save khg0712/de05423fbf54bb0c884cb051721a9a55 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// packages/next/server/render.tsx | |
// pre-render하는 renderToHTML 함수 | |
// https://github.com/vercel/next.js/blob/v12.3.4/packages/next/server/render.tsx#L351 | |
export async function renderToHTML( | |
req: IncomingMessage, | |
res: ServerResponse, | |
pathname: string, | |
query: NextParsedUrlQuery, | |
renderOpts: RenderOpts | |
): Promise<RenderResult | null> { | |
// ... | |
// SSG 여부 구분 | |
const isSSG = !!getStaticProps | |
// ... | |
// getServerSideProps fetch 처리 | |
if (getServerSideProps && !isFallback) { | |
// ... | |
try { | |
// getServerSideProps 실행 | |
data = await getServerSideProps({ | |
// ... | |
}); | |
} catch (serverSidePropsError: any) { | |
// ... | |
} | |
// ... | |
} | |
// ... | |
// document render | |
// _document.tsx, _app.tsx, render하려는 페이지가 통합되어 render됨 | |
const documentResult = await renderDocument() | |
// ... | |
// documentResult로 stream 만들기 | |
const streams = [ | |
streamFromArray(prefix), | |
await documentResult.bodyResult(renderTargetSuffix), | |
] | |
// 최적화하기 | |
const postOptimize = (html: string) => | |
postProcessHTML(pathname, html, renderOpts, { inAmpMode, hybridAmp }) | |
// generateStaticHTML에 따라 pre-render 결과물이 달라짐 | |
if (generateStaticHTML) { | |
const html = await streamToString(chainStreams(streams)) | |
const optimizedHtml = await postOptimize(html) | |
return new RenderResult(optimizedHtml) | |
} | |
return new RenderResult( | |
chainStreams(streams).pipeThrough( | |
createBufferedTransformStream(postOptimize) | |
) | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment