Unverified Commit 72e5b8db authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Consolidate non-SSG branch in `renderToHTMLWithComponents` Cont. (#13274)

This continues https://github.com/zeit/next.js/pull/12501 and resolves the merge conflicts since we aren't able to update that branch and would like to land this change

Closes: https://github.com/zeit/next.js/pull/12501
parent dc1658f1
...@@ -969,70 +969,6 @@ export default class Server { ...@@ -969,70 +969,6 @@ export default class Server {
isPreviewMode = previewData !== false isPreviewMode = previewData !== false
} }
// non-spr requests should render like normal
if (!isSSG) {
// handle serverless
if (isLikeServerless) {
if (isDataReq) {
const renderResult = await (components.Component as any).renderReqToHTML(
req,
res,
'passthrough'
)
sendPayload(
res,
JSON.stringify(renderResult?.renderOpts?.pageData),
'json',
!this.renderOpts.dev
? {
private: isPreviewMode,
stateful: true, // non-SSG data request
}
: undefined
)
return null
}
prepareServerlessUrl(req, query)
return (components.Component as any).renderReqToHTML(req, res)
}
if (isDataReq && isServerProps) {
const props = await renderToHTML(req, res, pathname, query, {
...components,
...opts,
isDataReq,
})
sendPayload(
res,
JSON.stringify(props),
'json',
!this.renderOpts.dev
? {
private: isPreviewMode,
stateful: true, // GSSP data request
}
: undefined
)
return null
}
const html = await renderToHTML(req, res, pathname, query, {
...components,
...opts,
})
if (html && isServerProps) {
sendPayload(res, html, 'html', {
private: isPreviewMode,
stateful: true, // GSSP request
})
return null
}
return html
}
// Compute the iSSG cache key // Compute the iSSG cache key
let urlPathname = `${parseUrl(req.url || '').pathname!}` let urlPathname = `${parseUrl(req.url || '').pathname!}`
...@@ -1044,7 +980,8 @@ export default class Server { ...@@ -1044,7 +980,8 @@ export default class Server {
.replace(/\/index$/, '/') .replace(/\/index$/, '/')
} }
const ssgCacheKey = isPreviewMode const ssgCacheKey =
isPreviewMode || !isSSG
? undefined // Preview mode bypasses the cache ? undefined // Preview mode bypasses the cache
: `${urlPathname}${query.amp ? '.amp' : ''}` : `${urlPathname}${query.amp ? '.amp' : ''}`
...@@ -1148,6 +1085,7 @@ export default class Server { ...@@ -1148,6 +1085,7 @@ export default class Server {
// getStaticPaths, then finish the data request on the client-side. // getStaticPaths, then finish the data request on the client-side.
// //
if ( if (
ssgCacheKey &&
!didRespond && !didRespond &&
!isDataReq && !isDataReq &&
!isPreviewMode && !isPreviewMode &&
...@@ -1177,18 +1115,9 @@ export default class Server { ...@@ -1177,18 +1115,9 @@ export default class Server {
query.__nextFallback = 'true' query.__nextFallback = 'true'
if (isLikeServerless) { if (isLikeServerless) {
prepareServerlessUrl(req, query) prepareServerlessUrl(req, query)
const renderResult = await (components.Component as any).renderReqToHTML(
req,
res,
'passthrough'
)
html = renderResult.html
} else {
html = (await renderToHTML(req, res, pathname, query, {
...components,
...opts,
})) as string
} }
const { value: renderResult } = await doRender()
html = renderResult.html
} }
sendPayload(res, html, 'html') sendPayload(res, html, 'html')
...@@ -1198,19 +1127,21 @@ export default class Server { ...@@ -1198,19 +1127,21 @@ export default class Server {
isOrigin, isOrigin,
value: { html, pageData, sprRevalidate }, value: { html, pageData, sprRevalidate },
} = await doRender() } = await doRender()
if (!isResSent(res)) { let resHtml = html
if (!isResSent(res) && (isSSG || isDataReq || isServerProps)) {
sendPayload( sendPayload(
res, res,
isDataReq ? JSON.stringify(pageData) : html, isDataReq ? JSON.stringify(pageData) : html,
isDataReq ? 'json' : 'html', isDataReq ? 'json' : 'html',
!this.renderOpts.dev !this.renderOpts.dev || (isServerProps && !isDataReq)
? { ? {
private: isPreviewMode, private: isPreviewMode,
stateful: false, // GSP response stateful: !isSSG,
revalidate: sprRevalidate, revalidate: sprRevalidate,
} }
: undefined : undefined
) )
resHtml = null
} }
// Update the SPR cache if the head request and cacheable // Update the SPR cache if the head request and cacheable
...@@ -1218,7 +1149,7 @@ export default class Server { ...@@ -1218,7 +1149,7 @@ export default class Server {
await setSprCache(ssgCacheKey, { html: html!, pageData }, sprRevalidate) await setSprCache(ssgCacheKey, { html: html!, pageData }, sprRevalidate)
} }
return null return resHtml
} }
public async renderToHTML( public async renderToHTML(
......
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