Skip to content

Commit

Permalink
Revert support for render prop in <Main /> (#32184)
Browse files Browse the repository at this point in the history
Reverts support for render prop in `<Main />` in `Document`. This was added just to support the `useFlushEffect` hook, but we've tweaked the design a bit, moving it to `App`.
  • Loading branch information
devknoll committed Dec 6, 2021
1 parent 1fe014c commit 9bcf678
Show file tree
Hide file tree
Showing 9 changed files with 11 additions and 121 deletions.
12 changes: 4 additions & 8 deletions packages/next/pages/_document.tsx
Expand Up @@ -756,15 +756,11 @@ export class Head extends Component<
}
}

export function Main({
children,
}: {
children?: (content: JSX.Element) => JSX.Element
}) {
const { docComponentsRendered, useMainContent } = useContext(HtmlContext)
const content = useMainContent(children)
export function Main() {
const { docComponentsRendered } = useContext(HtmlContext)
docComponentsRendered.Main = true
return content
// @ts-ignore
return <next-js-internal-body-render-target />
}

export class NextScript extends Component<OriginProps> {
Expand Down
45 changes: 7 additions & 38 deletions packages/next/server/render.tsx
Expand Up @@ -1048,20 +1048,6 @@ export async function renderToHTML(
return inAmpMode ? children : <div id="__next">{children}</div>
}

const appWrappers: Array<(content: JSX.Element) => JSX.Element> = []
const getWrappedApp = (app: JSX.Element) => {
// Prevent wrappers from reading/writing props by rendering inside an
// opaque component. Wrappers should use context instead.
const InnerApp = () => app
return (
<AppContainerWithIsomorphicFiberStructure>
{appWrappers.reduceRight((innerContent, fn) => {
return fn(innerContent)
}, <InnerApp />)}
</AppContainerWithIsomorphicFiberStructure>
)
}

/**
* Rules of Static & Dynamic HTML:
*
Expand Down Expand Up @@ -1106,13 +1092,13 @@ export async function renderToHTML(

const html = ReactDOMServer.renderToString(
<Body>
{getWrappedApp(
<AppContainerWithIsomorphicFiberStructure>
<EnhancedApp
Component={EnhancedComponent}
router={router}
{...props}
/>
)}
</AppContainerWithIsomorphicFiberStructure>
</Body>
)
return { html, head }
Expand All @@ -1137,15 +1123,6 @@ export async function renderToHTML(
documentElement: (htmlProps: HtmlProps) => (
<Document {...htmlProps} {...docProps} />
),
useMainContent: (fn?: (content: JSX.Element) => JSX.Element) => {
if (fn) {
throw new Error(
'The `children` property is not supported by non-functional custom Document components'
)
}
// @ts-ignore
return <next-js-internal-body-render-target />
},
head: docProps.head,
headTags: await headTags(documentCtx),
styles: docProps.styles,
Expand All @@ -1164,9 +1141,9 @@ export async function renderToHTML(
</Body>
) : (
<Body>
{getWrappedApp(
<AppContainerWithIsomorphicFiberStructure>
<App {...props} Component={Component} router={router} />
)}
</AppContainerWithIsomorphicFiberStructure>
</Body>
)
return process.browser
Expand All @@ -1181,9 +1158,9 @@ export async function renderToHTML(
</Body>
) : (
<Body>
{getWrappedApp(
<AppContainerWithIsomorphicFiberStructure>
<App {...props} Component={Component} router={router} />
)}
</AppContainerWithIsomorphicFiberStructure>
</Body>
)
// for non-concurrent rendering we need to ensure App is rendered
Expand All @@ -1196,13 +1173,6 @@ export async function renderToHTML(
return {
bodyResult,
documentElement: () => (Document as any)(),
useMainContent: (fn?: (_content: JSX.Element) => JSX.Element) => {
if (fn) {
appWrappers.push(fn)
}
// @ts-ignore
return <next-js-internal-body-render-target />
},
head,
headTags: [],
styles: jsxStyleRegistry.styles(),
Expand Down Expand Up @@ -1243,7 +1213,7 @@ export async function renderToHTML(
locales,
runtimeConfig,
} = renderOpts
const htmlProps: any = {
const htmlProps: HtmlProps = {
__NEXT_DATA__: {
props, // The result of getInitialProps
page: pathname, // The rendered page
Expand Down Expand Up @@ -1297,7 +1267,6 @@ export async function renderToHTML(
head: documentResult.head,
headTags: documentResult.headTags,
styles: documentResult.styles,
useMainContent: documentResult.useMainContent,
useMaybeDeferContent,
crossOrigin: renderOpts.crossOrigin,
optimizeCss: renderOpts.optimizeCss,
Expand Down
1 change: 0 additions & 1 deletion packages/next/shared/lib/utils.ts
Expand Up @@ -221,7 +221,6 @@ export type HtmlProps = {
styles?: React.ReactElement[] | React.ReactFragment
head?: Array<JSX.Element | null>
useMaybeDeferContent: MaybeDeferContentHook
useMainContent: (fn?: (content: JSX.Element) => JSX.Element) => JSX.Element
crossOrigin?: string
optimizeCss?: boolean
optimizeFonts?: boolean
Expand Down

This file was deleted.

This file was deleted.

12 changes: 0 additions & 12 deletions test/integration/document-functional-render-prop/app/package.json

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 9bcf678

Please sign in to comment.