Skip to content

Commit

Permalink
refactor: merge render methods for web server
Browse files Browse the repository at this point in the history
  • Loading branch information
wyattjoh committed Jun 15, 2023
1 parent f4b730b commit 50ed751
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,16 @@ const edgeSSRLoader: webpack.LoaderDefinitionFunction<EdgeSSRLoaderQuery> =
${
isAppDir
? `
import { renderToHTMLOrFlight as appRenderToHTML } from 'next/dist/esm/server/app-render/app-render'
import { renderToHTMLOrFlight as renderToHTML } from 'next/dist/esm/server/app-render/app-render'
import * as pageMod from ${JSON.stringify(pageModPath)}
const Document = null
const pagesRenderToHTML = null
const appMod = null
const errorMod = null
const error500Mod = null
`
: `
import Document from ${stringifiedDocumentPath}
import { renderToHTML as pagesRenderToHTML } from 'next/dist/esm/server/render'
import { renderToHTML } from 'next/dist/esm/server/render'
import * as pageMod from ${stringifiedPagePath}
import * as appMod from ${stringifiedAppPath}
import * as errorMod from ${stringifiedErrorPath}
Expand All @@ -135,7 +134,6 @@ const edgeSSRLoader: webpack.LoaderDefinitionFunction<EdgeSSRLoaderQuery> =
? `import * as error500Mod from ${stringified500Path}`
: `const error500Mod = null`
}
const appRenderToHTML = null
`
}
Expand Down Expand Up @@ -171,8 +169,7 @@ const edgeSSRLoader: webpack.LoaderDefinitionFunction<EdgeSSRLoaderQuery> =
buildManifest,
isAppPath: ${!!isAppDir},
prerenderManifest,
appRenderToHTML,
pagesRenderToHTML,
renderToHTML,
reactLoadableManifest,
clientReferenceManifest: ${isServerComponent} ? rscManifest : null,
serverActionsManifest: ${isServerComponent} ? rscServerManifest : null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ export function getRender({
buildManifest,
prerenderManifest,
reactLoadableManifest,
appRenderToHTML,
pagesRenderToHTML,
renderToHTML,
clientReferenceManifest,
subresourceIntegrityManifest,
serverActionsManifest,
Expand All @@ -44,8 +43,7 @@ export function getRender({
pageMod: any
errorMod: any
error500Mod: any
appRenderToHTML: any
pagesRenderToHTML: any
renderToHTML: any
Document: DocumentType
buildManifest: BuildManifest
prerenderManifest: PrerenderManifest
Expand Down Expand Up @@ -88,8 +86,7 @@ export function getRender({
clientReferenceManifest,
serverActionsManifest,
},
appRenderToHTML,
pagesRenderToHTML,
renderToHTML,
incrementalCacheHandler,
loadComponent: async (pathname) => {
if (pathname === page) {
Expand Down Expand Up @@ -137,12 +134,15 @@ export function getRender({
},
},
})
const requestHandler = server.getRequestHandler()

const handler = server.getRequestHandler()

return async function render(request: Request) {
const extendedReq = new WebNextRequest(request)
const extendedRes = new WebNextResponse()
requestHandler(extendedReq, extendedRes)

handler(extendedReq, extendedRes)

return await extendedRes.toResponse()
}
}
38 changes: 17 additions & 21 deletions packages/next/src/server/web-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ interface WebServerOptions extends Options {
) => Promise<LoadComponentsReturnType | null>
extendRenderOpts: Partial<BaseServer['renderOpts']> &
Pick<BaseServer['renderOpts'], 'buildId'>
pagesRenderToHTML?: typeof import('./render').renderToHTML
appRenderToHTML?: typeof import('./app-render/app-render').renderToHTMLOrFlight
renderToHTML:
| typeof import('./render').renderToHTML
| typeof import('./app-render/app-render').renderToHTMLOrFlight
incrementalCacheHandler?: any
prerenderManifest: PrerenderManifest | undefined
}
Expand Down Expand Up @@ -362,32 +363,27 @@ export default class NextWebServer extends BaseServer<WebServerOptions> {
return false
}

protected async renderHTML(
protected renderHTML(
req: WebNextRequest,
res: WebNextResponse,
pathname: string,
query: NextParsedUrlQuery,
renderOpts: RenderOpts
): Promise<RenderResult> {
const { pagesRenderToHTML, appRenderToHTML } =
this.serverOptions.webServerConfig
const curRenderToHTML = pagesRenderToHTML || appRenderToHTML

if (curRenderToHTML) {
return await curRenderToHTML(
req as any,
res as any,
pathname,
query,
Object.assign(renderOpts, {
disableOptimizedLoading: true,
runtime: 'experimental-edge',
})
)
} else {
throw new Error(`Invariant: curRenderToHTML is missing`)
}
const { renderToHTML } = this.serverOptions.webServerConfig

return renderToHTML(
req as any,
res as any,
pathname,
query,
Object.assign(renderOpts, {
disableOptimizedLoading: true,
runtime: 'experimental-edge',
})
)
}

protected async sendRenderResult(
_req: WebNextRequest,
res: WebNextResponse,
Expand Down

0 comments on commit 50ed751

Please sign in to comment.