Unverified Commit 9b22dc46 authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Improve custom document error message (#17048)

parent 9adf3bb6
......@@ -2,6 +2,7 @@ import { IncomingMessage, ServerResponse } from 'http'
import { ParsedUrlQuery } from 'querystring'
import React from 'react'
import { renderToStaticMarkup, renderToString } from 'react-dom/server'
import { warn } from '../../build/output/log'
import { UnwrapPromise } from '../../lib/coalesced-function'
import {
GSP_NO_RETURNED_VALUE,
......@@ -19,9 +20,9 @@ import { isInAmpMode } from '../lib/amp'
import { AmpStateContext } from '../lib/amp-context'
import {
AMP_RENDER_TARGET,
PERMANENT_REDIRECT_STATUS,
SERVER_PROPS_ID,
STATIC_PROPS_ID,
PERMANENT_REDIRECT_STATUS,
TEMPORARY_REDIRECT_STATUS,
} from '../lib/constants'
import { defaultHead } from '../lib/head'
......@@ -37,13 +38,13 @@ import {
AppType,
ComponentsEnhancer,
DocumentInitialProps,
DocumentProps,
DocumentType,
getDisplayName,
isResSent,
loadGetInitialProps,
NextComponentType,
RenderPage,
DocumentProps,
} from '../lib/utils'
import { tryGetPreviewData, __ApiPreviewProps } from './api-utils'
import { denormalizePagePath } from './denormalize-page-path'
......@@ -906,13 +907,13 @@ export async function renderToHTML(
const plural = nonRenderedComponents.length !== 1 ? 's' : ''
if (nonRenderedComponents.length) {
console.warn(
`Expected Document Component${plural} ${nonRenderedComponents.join(
', '
)} ${
plural ? 'were' : 'was'
} not rendered. Make sure you render them in your custom \`_document\`\n` +
`See more info here https://err.sh/next.js/missing-document-component`
const missingComponentList = nonRenderedComponents
.map((e) => `<${e} />`)
.join(', ')
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://err.sh/next.js/missing-document-component'
)
}
}
......
......@@ -40,7 +40,7 @@ const checkMissing = async (missing = [], docContent) => {
describe('Missing _document components error', () => {
it('should detect missing Html component', async () => {
await checkMissing(
['Html'],
['<Html />'],
`
import Document, { Head, Main, NextScript } from 'next/document'
......@@ -65,7 +65,7 @@ describe('Missing _document components error', () => {
it('should detect missing Head component', async () => {
await checkMissing(
['Head'],
['<Head />'],
`
import Document, { Html, Main, NextScript } from 'next/document'
......@@ -89,7 +89,7 @@ describe('Missing _document components error', () => {
it('should detect missing Main component', async () => {
await checkMissing(
['Main'],
['<Main />'],
`
import Document, { Html, Head, NextScript } from 'next/document'
......@@ -113,7 +113,7 @@ describe('Missing _document components error', () => {
it('should detect missing NextScript component', async () => {
await checkMissing(
['NextScript'],
['<NextScript />'],
`
import Document, { Html, Head, Main } from 'next/document'
......@@ -136,7 +136,7 @@ describe('Missing _document components error', () => {
it('should detect multiple missing document components', async () => {
await checkMissing(
['Head', 'NextScript'],
['<Head />', '<NextScript />'],
`
import Document, { Html, Main } from 'next/document'
......
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