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