Unverified Commit ab712ef3 authored by Gerald Monaco's avatar Gerald Monaco Committed by GitHub

Move root div to an app wrapper (#31596)

Extracted from #31223.

We need to move the root `<div id="__next">` wrapper to be rendered as part of the page content, rather than the`Document`, so that flush effects (like styles) are flushed before (or after) the div, rather than inside, where they would cause hydration mismatches.
parent d1ce7b7c
...@@ -766,13 +766,10 @@ export function Main({ ...@@ -766,13 +766,10 @@ export function Main({
}: { }: {
children?: (content: JSX.Element) => JSX.Element children?: (content: JSX.Element) => JSX.Element
}) { }) {
const { inAmpMode, docComponentsRendered, useMainContent } = const { docComponentsRendered, useMainContent } = useContext(HtmlContext)
useContext(HtmlContext)
const content = useMainContent(children) const content = useMainContent(children)
docComponentsRendered.Main = true docComponentsRendered.Main = true
return content
if (inAmpMode) return content
return <div id="__next">{content}</div>
} }
export class NextScript extends Component<OriginProps> { export class NextScript extends Component<OriginProps> {
......
...@@ -1041,6 +1041,10 @@ export async function renderToHTML( ...@@ -1041,6 +1041,10 @@ export async function renderToHTML(
} }
} }
const Body = ({ children }: { children: JSX.Element }) => {
return inAmpMode ? children : <div id="__next">{children}</div>
}
const appWrappers: Array<(content: JSX.Element) => JSX.Element> = [] const appWrappers: Array<(content: JSX.Element) => JSX.Element> = []
const getWrappedApp = (app: JSX.Element) => { const getWrappedApp = (app: JSX.Element) => {
// Prevent wrappers from reading/writing props by rendering inside an // Prevent wrappers from reading/writing props by rendering inside an
...@@ -1048,7 +1052,7 @@ export async function renderToHTML( ...@@ -1048,7 +1052,7 @@ export async function renderToHTML(
const InnerApp = () => app const InnerApp = () => app
return ( return (
<AppContainerWithIsomorphicFiberStructure> <AppContainerWithIsomorphicFiberStructure>
{appWrappers.reduce((innerContent, fn) => { {appWrappers.reduceRight((innerContent, fn) => {
return fn(innerContent) return fn(innerContent)
}, <InnerApp />)} }, <InnerApp />)}
</AppContainerWithIsomorphicFiberStructure> </AppContainerWithIsomorphicFiberStructure>
...@@ -1081,7 +1085,9 @@ export async function renderToHTML( ...@@ -1081,7 +1085,9 @@ export async function renderToHTML(
): RenderPageResult | Promise<RenderPageResult> => { ): RenderPageResult | Promise<RenderPageResult> => {
if (ctx.err && ErrorDebug) { if (ctx.err && ErrorDebug) {
const html = ReactDOMServer.renderToString( const html = ReactDOMServer.renderToString(
<ErrorDebug error={ctx.err} /> <Body>
<ErrorDebug error={ctx.err} />
</Body>
) )
return { html, head } return { html, head }
} }
...@@ -1096,13 +1102,15 @@ export async function renderToHTML( ...@@ -1096,13 +1102,15 @@ export async function renderToHTML(
enhanceComponents(options, App, Component) enhanceComponents(options, App, Component)
const html = ReactDOMServer.renderToString( const html = ReactDOMServer.renderToString(
getWrappedApp( <Body>
<EnhancedApp {getWrappedApp(
Component={EnhancedComponent} <EnhancedApp
router={router} Component={EnhancedComponent}
{...props} router={router}
/> {...props}
) />
)}
</Body>
) )
return { html, head } return { html, head }
} }
...@@ -1141,14 +1149,17 @@ export async function renderToHTML( ...@@ -1141,14 +1149,17 @@ export async function renderToHTML(
} }
} else { } else {
const bodyResult = async () => { const bodyResult = async () => {
const content = const content = (
ctx.err && ErrorDebug ? ( <Body>
<ErrorDebug error={ctx.err} /> {ctx.err && ErrorDebug ? (
) : ( <ErrorDebug error={ctx.err} />
getWrappedApp( ) : (
<App {...props} Component={Component} router={router} /> getWrappedApp(
) <App {...props} Component={Component} router={router} />
) )
)}
</Body>
)
return concurrentFeatures return concurrentFeatures
? process.browser ? process.browser
......
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