Skip to content
This repository has been archived by the owner on Jan 22, 2022. It is now read-only.

Commit

Permalink
Fixes vercel#32338 missing Document components trigger an error for p…
Browse files Browse the repository at this point in the history
…roduction builds (vercel#32345)

* Only validate _document subcomponents in dev environment

* Replace error by a warning

Co-authored-by: JJ Kasper <jj@jjsweb.site>
  • Loading branch information
2 people authored and cdierkens committed Dec 20, 2021
1 parent 3a0b74f commit c7704cf
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions packages/next/server/render.tsx
Expand Up @@ -1375,25 +1375,27 @@ export async function renderToHTML(
documentHTML = ReactDOMServer.renderToStaticMarkup(document)
}

const nonRenderedComponents = []
const expectedDocComponents = ['Main', 'Head', 'NextScript', 'Html']
if (process.env.NODE_ENV !== 'production') {
const nonRenderedComponents = []
const expectedDocComponents = ['Main', 'Head', 'NextScript', 'Html']

for (const comp of expectedDocComponents) {
if (!(docComponentsRendered as any)[comp]) {
nonRenderedComponents.push(comp)
for (const comp of expectedDocComponents) {
if (!(docComponentsRendered as any)[comp]) {
nonRenderedComponents.push(comp)
}
}
}

if (nonRenderedComponents.length) {
const missingComponentList = nonRenderedComponents
.map((e) => `<${e} />`)
.join(', ')
const plural = nonRenderedComponents.length !== 1 ? 's' : ''
throw new Error(
`Your custom Document (pages/_document) did not render all the required subcomponent${plural}.\n` +
`Missing component${plural}: ${missingComponentList}\n` +
'Read how to fix here: https://nextjs.org/docs/messages/missing-document-component'
)
if (nonRenderedComponents.length) {
const missingComponentList = nonRenderedComponents
.map((e) => `<${e} />`)
.join(', ')
const plural = nonRenderedComponents.length !== 1 ? 's' : ''
console.warn(
`Your custom Document (pages/_document) did not render all the required subcomponent${plural}.\n` +
`Missing component${plural}: ${missingComponentList}\n` +
'Read how to fix here: https://nextjs.org/docs/messages/missing-document-component'
)
}
}

const [renderTargetPrefix, renderTargetSuffix] = documentHTML.split(
Expand Down

0 comments on commit c7704cf

Please sign in to comment.