Unverified Commit be9491e2 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Ensure there is only 1 render pass in concurrent rendering with getInitialProps…

Ensure there is only 1 render pass in concurrent rendering with getInitialProps in `_document` (#36352)

This PR makes sure `renderPage` calls `renderShell` in concurrent features, and `renderToString` if not.

Closes #36268, #36229.

## Bug

- [x] Related issues linked using `fixes #number`
- [x] 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`
parent b8f7c520
......@@ -395,7 +395,6 @@ const useFlightResponse = createFlightHook()
// Create the wrapper component for a Flight stream.
function createServerComponentRenderer(
AppMod: any,
ComponentMod: any,
{
cachePrefix,
......@@ -415,10 +414,13 @@ function createServerComponentRenderer(
globalThis.__webpack_require__ = ComponentMod.__next_rsc__.__webpack_require__
const Component = interopDefault(ComponentMod)
function ServerComponentWrapper(props: any) {
function ServerComponentWrapper({ App, router, ...props }: any) {
const id = (React as any).useId()
const reqStream: ReadableStream<Uint8Array> = renderToReadableStream(
renderFlight(AppMod, ComponentMod, props),
<App>
<Component {...props} />
</App>,
serverComponentManifest
)
......@@ -520,7 +522,7 @@ export async function renderToHTML(
if (isServerComponent) {
serverComponentsInlinedTransformStream = new TransformStream()
const search = urlQueryToSearchParams(query).toString()
Component = createServerComponentRenderer(AppMod, ComponentMod, {
Component = createServerComponentRenderer(ComponentMod, {
cachePrefix: pathname + (search ? `?${search}` : ''),
inlinedTransformStream: serverComponentsInlinedTransformStream,
staticTransformStream: serverComponentsPageDataTransformStream,
......@@ -1311,11 +1313,24 @@ export async function renderToHTML(
}
}
async function documentInitialProps() {
async function documentInitialProps(
renderShell?: ({
EnhancedApp,
EnhancedComponent,
}: {
EnhancedApp?: AppType
EnhancedComponent?: NextComponentType
}) => Promise<void>
) {
const renderPage: RenderPage = (
options: ComponentsEnhancer = {}
): RenderPageResult | Promise<RenderPageResult> => {
if (ctx.err && ErrorDebug) {
// Always start rendering the shell even if there's an error.
if (renderShell) {
renderShell({})
}
const html = ReactDOMServer.renderToString(
<Body>
<ErrorDebug error={ctx.err} />
......@@ -1333,6 +1348,14 @@ export async function renderToHTML(
const { App: EnhancedApp, Component: EnhancedComponent } =
enhanceComponents(options, App, Component)
if (renderShell) {
return renderShell({ EnhancedApp, EnhancedComponent }).then(() => {
// When using concurrent features, we don't have or need the full
// html so it's fine to return nothing here.
return { html: '', head }
})
}
const html = ReactDOMServer.renderToString(
<Body>
<AppContainerWithIsomorphicFiberStructure>
......@@ -1364,7 +1387,13 @@ export async function renderToHTML(
return { docProps, documentCtx }
}
const renderContent = () => {
const renderContent = ({
EnhancedApp,
EnhancedComponent,
}: {
EnhancedApp?: AppType
EnhancedComponent?: NextComponentType
} = {}) => {
return ctx.err && ErrorDebug ? (
<Body>
<ErrorDebug error={ctx.err} />
......@@ -1372,12 +1401,16 @@ export async function renderToHTML(
) : (
<Body>
<AppContainerWithIsomorphicFiberStructure>
{isServerComponent && !!AppMod.__next_rsc__ ? (
// _app.server.js is used.
<Component {...props.pageProps} />
) : (
<App {...props} Component={Component} router={router} />
)}
{isServerComponent
? React.createElement(EnhancedComponent || Component, {
App: EnhancedApp || App,
...props.pageProps,
})
: React.createElement(EnhancedApp || App, {
...props,
Component: EnhancedComponent || Component,
router,
})}
</AppContainerWithIsomorphicFiberStructure>
</Body>
)
......@@ -1419,13 +1452,23 @@ export async function renderToHTML(
}
}
} else {
// We start rendering the shell earlier, before returning the head tags
// to `documentResult`.
const content = renderContent()
const renderStream = await renderToInitialStream({
ReactDOMServer,
element: content,
})
let renderStream: ReadableStream<Uint8Array> & {
allReady?: Promise<void> | undefined
}
const renderShell = async ({
EnhancedApp,
EnhancedComponent,
}: {
EnhancedApp?: AppType
EnhancedComponent?: NextComponentType
} = {}) => {
const content = renderContent({ EnhancedApp, EnhancedComponent })
renderStream = await renderToInitialStream({
ReactDOMServer,
element: content,
})
}
const bodyResult = async (suffix: string) => {
// this must be called inside bodyResult so appWrappers is
......@@ -1494,10 +1537,18 @@ export async function renderToHTML(
!Document.getInitialProps
)
const documentInitialPropsRes = hasDocumentGetInitialProps
? await documentInitialProps()
: {}
if (documentInitialPropsRes === null) return null
// If it has getInitialProps, we will render the shell in `renderPage`.
// Otherwise we do it right now.
let documentInitialPropsRes:
| {}
| Awaited<ReturnType<typeof documentInitialProps>>
if (hasDocumentGetInitialProps) {
documentInitialPropsRes = await documentInitialProps(renderShell)
if (documentInitialPropsRes === null) return null
} else {
await renderShell()
documentInitialPropsRes = {}
}
const { docProps } = (documentInitialPropsRes as any) || {}
const documentElement = () => {
......
......@@ -73,4 +73,15 @@ describe('styled-components SWC transform', () => {
expect(html).toContain('background:transparent')
expect(html).toContain('color:white')
})
it('should only render once on the server per request', async () => {
const outputs = []
next.on('stdout', (args) => {
outputs.push(args)
})
await renderViaHTTP(next.url, '/')
expect(
outputs.filter((output) => output.trim() === '__render__').length
).toBe(1)
})
})
......@@ -21,6 +21,7 @@ const Button = styled.a`
`
export default function Home() {
console.log('__render__')
return (
<div>
<Button
......
......@@ -5,6 +5,7 @@ export default function Index() {
if (typeof window !== 'undefined') {
window.didHydrate = true
}
console.log('__render__')
return (
<div>
<p id="react-dom-version">{ReactDOM.version}</p>
......
......@@ -5,6 +5,11 @@ import cheerio from 'cheerio'
import { renderViaHTTP } from 'next-test-utils'
export default (context, env) => {
it('should only render once in SSR', async () => {
await renderViaHTTP(context.appPort, '/')
expect([...context.stdout.matchAll(/__render__/g)].length).toBe(1)
})
it('no warnings for image related link props', async () => {
await renderViaHTTP(context.appPort, '/')
expect(context.stderr).not.toContain('Warning: Invalid DOM property')
......
......@@ -758,6 +758,10 @@ function runSuite(suiteName, context, options) {
const onStderr = (msg) => {
context.stderr += msg
}
context.stdout = ''
const onStdout = (msg) => {
context.stdout += msg
}
if (env === 'prod') {
context.appPort = await findPort()
const { stdout, stderr, code } = await nextBuild(appDir, [], {
......@@ -769,11 +773,13 @@ function runSuite(suiteName, context, options) {
context.code = code
context.server = await nextStart(context.appDir, context.appPort, {
onStderr,
onStdout,
})
} else if (env === 'dev') {
context.appPort = await findPort()
context.server = await launchApp(context.appDir, context.appPort, {
onStderr,
onStdout,
})
}
})
......
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