Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the special _document-concurrent component #35242

Merged
merged 8 commits into from Mar 11, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 1 addition & 5 deletions packages/next/build/entries.ts
Expand Up @@ -39,11 +39,9 @@ export function createPagesMapping(
{
isDev,
hasServerComponents,
globalRuntime,
}: {
isDev: boolean
hasServerComponents: boolean
globalRuntime?: 'nodejs' | 'edge'
}
): PagesMapping {
const previousPages: PagesMapping = {}
Expand Down Expand Up @@ -83,16 +81,14 @@ export function createPagesMapping(
// we alias these in development and allow webpack to
// allow falling back to the correct source file so
// that HMR can work properly when a file is added/removed
const documentPage = `_document${globalRuntime ? '-concurrent' : ''}`
if (isDev) {
pages['/_app'] = `${PAGES_DIR_ALIAS}/_app`
pages['/_error'] = `${PAGES_DIR_ALIAS}/_error`
pages['/_document'] = `${PAGES_DIR_ALIAS}/_document`
} else {
pages['/_app'] = pages['/_app'] || 'next/dist/pages/_app'
pages['/_error'] = pages['/_error'] || 'next/dist/pages/_error'
pages['/_document'] =
pages['/_document'] || `next/dist/pages/${documentPage}`
pages['/_document'] = pages['/_document'] || `next/dist/pages/_document`
}
return pages
}
Expand Down
1 change: 0 additions & 1 deletion packages/next/build/index.ts
Expand Up @@ -298,7 +298,6 @@ export default async function build(
createPagesMapping(pagePaths, config.pageExtensions, {
isDev: false,
hasServerComponents,
globalRuntime: runtime,
})
)

Expand Down
4 changes: 1 addition & 3 deletions packages/next/build/webpack-config.ts
Expand Up @@ -558,9 +558,7 @@ export default async function getBaseWebpackConfig(
prev.push(path.join(pagesDir, `_document.${ext}`))
return prev
}, [] as string[]),
`next/dist/pages/_document${
hasConcurrentFeatures ? '-concurrent' : ''
}.js`,
`next/dist/pages/_document.js`,
]
}

Expand Down
16 changes: 0 additions & 16 deletions packages/next/pages/_document-concurrent.tsx

This file was deleted.

15 changes: 15 additions & 0 deletions packages/next/pages/_document.tsx
Expand Up @@ -184,6 +184,21 @@ export default class Document<P = {}> extends Component<DocumentProps & P> {
}
}

// Add a speical property to the built-in `Document` component so later we can
// identify if a user customized `Document` is used or not.
;(Document as any).__next_internal_document =
function InternalFunctionDocument() {
return (
<Html>
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
)
}

export function Html(
props: React.DetailedHTMLProps<
React.HtmlHTMLAttributes<HTMLHtmlElement>,
Expand Down
6 changes: 5 additions & 1 deletion packages/next/server/base-server.ts
Expand Up @@ -1176,14 +1176,18 @@ export default abstract class Server {

if (opts.supportsDynamicHTML === true) {
const isBotRequest = isBot(req.headers['user-agent'] || '')
const isSupportedDocument =
(components.Document as any)?.__next_internal_document ||
typeof components.Document?.getInitialProps !== 'function'

// Disable dynamic HTML in cases that we know it won't be generated,
// so that we can continue generating a cache key when possible.
opts.supportsDynamicHTML =
!isSSG &&
!isLikeServerless &&
!isBotRequest &&
!query.amp &&
typeof components.Document?.getInitialProps !== 'function'
isSupportedDocument
}

const defaultLocale = isSSG
Expand Down
1 change: 0 additions & 1 deletion packages/next/server/dev/hot-reloader.ts
Expand Up @@ -321,7 +321,6 @@ export default class HotReloader {
this.config.pageExtensions,
{
isDev: true,
globalRuntime: this.runtime,
hasServerComponents: this.hasServerComponents,
}
)
Expand Down
4 changes: 1 addition & 3 deletions packages/next/server/dev/next-dev-server.ts
Expand Up @@ -949,9 +949,7 @@ export default class DevServer extends Server {
// Build the error page to ensure the fallback is built too.
// TODO: See if this can be moved into hotReloader or removed.
await this.hotReloader!.ensurePage('/_error')
return await loadDefaultErrorComponents(this.distDir, {
hasConcurrentFeatures: !!this.renderOpts.runtime,
})
return await loadDefaultErrorComponents(this.distDir)
}

protected setImmutableAssetCacheControl(res: BaseNextResponse): void {
Expand Down
10 changes: 2 additions & 8 deletions packages/next/server/load-components.ts
Expand Up @@ -39,14 +39,8 @@ export type LoadComponentsReturnType = {
AppMod: any
}

export async function loadDefaultErrorComponents(
distDir: string,
{ hasConcurrentFeatures }: { hasConcurrentFeatures: boolean }
) {
const Document = interopDefault(
require(`next/dist/pages/_document` +
(hasConcurrentFeatures ? '-concurrent' : ''))
)
export async function loadDefaultErrorComponents(distDir: string) {
const Document = interopDefault(require('next/dist/pages/_document'))
const AppMod = require('next/dist/pages/_app')
const App = interopDefault(AppMod)
const ComponentMod = require('next/dist/pages/_error')
Expand Down
28 changes: 22 additions & 6 deletions packages/next/server/render.tsx
Expand Up @@ -431,7 +431,6 @@ export async function renderToHTML(
dev = false,
ampPath = '',
App,
Document,
pageConfig = {},
buildManifest,
fontManifest,
Expand All @@ -457,6 +456,7 @@ export async function renderToHTML(

const hasConcurrentFeatures = !!runtime

let Document = renderOpts.Document
const OriginalComponent = renderOpts.Component

// We don't need to opt-into the flight inlining logic if the page isn't a RSC.
Expand Down Expand Up @@ -1234,14 +1234,30 @@ export async function renderToHTML(
*/
const generateStaticHTML = supportsDynamicHTML !== true
const renderDocument = async () => {
// For `Document`, there are two cases that we don't support:
// 1. Using `Document.getInitialProps` in the Edge runtime.
// 2. Using the class component `Document` with concurrent features.

const builtinDocument = (Document as any).__next_internal_document as
| typeof Document
| undefined

if (runtime === 'edge' && Document.getInitialProps) {
// In the Edge runtime, Document.getInitialProps isn't supported.
throw new Error(
'`getInitialProps` in Document component is not supported with the Edge Runtime.'
)
// In the Edge runtime, `Document.getInitialProps` isn't supported.
// We throw an error here if it's customized.
if (!builtinDocument) {
throw new Error(
'`getInitialProps` in Document component is not supported with the Edge Runtime.'
)
}
}

// We make it a function component to enable streaming.
if (hasConcurrentFeatures && builtinDocument) {
Document = builtinDocument
}

if (!runtime && Document.getInitialProps) {
if (!hasConcurrentFeatures && Document.getInitialProps) {
const renderPage: RenderPage = (
options: ComponentsEnhancer = {}
): RenderPageResult | Promise<RenderPageResult> => {
Expand Down