From 0cfa314d9b182b58a4c03bb646aa5244b68557ad Mon Sep 17 00:00:00 2001 From: Oriol Collell Date: Mon, 13 Dec 2021 23:47:34 +0100 Subject: [PATCH] Fixes #32338 missing Document components trigger an error for production builds (#32345) * Only validate _document subcomponents in dev environment * Replace error by a warning Co-authored-by: JJ Kasper --- packages/next/server/render.tsx | 34 +++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/packages/next/server/render.tsx b/packages/next/server/render.tsx index ce8044e05bea207..0249862b4b25894 100644 --- a/packages/next/server/render.tsx +++ b/packages/next/server/render.tsx @@ -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(