From 8fa0cb52dda68bd20078ed57b4f0461f3c1b01da Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Thu, 4 Jun 2020 15:40:51 +0200 Subject: [PATCH 1/6] Clean up render.tsx options --- .../webpack/plugins/build-manifest-plugin.ts | 3 +- packages/next/export/index.ts | 1 - packages/next/next-server/lib/utils.ts | 6 ++-- .../next/next-server/server/get-page-files.ts | 1 + .../next/next-server/server/next-server.ts | 4 --- packages/next/next-server/server/render.tsx | 35 +++---------------- packages/next/pages/_document.tsx | 23 ++++++------ packages/next/server/next.ts | 2 +- 8 files changed, 21 insertions(+), 54 deletions(-) diff --git a/packages/next/build/webpack/plugins/build-manifest-plugin.ts b/packages/next/build/webpack/plugins/build-manifest-plugin.ts index 3f7d1987e1149..d2a56ba5af4d7 100644 --- a/packages/next/build/webpack/plugins/build-manifest-plugin.ts +++ b/packages/next/build/webpack/plugins/build-manifest-plugin.ts @@ -56,6 +56,7 @@ export default class BuildManifestPlugin { (compilation, callback) => { const { chunks } = compilation const assetMap: BuildManifest = { + polyfillFiles: [], devFiles: [], lowPriorityFiles: [], pages: { '/_app': [] }, @@ -124,7 +125,7 @@ export default class BuildManifestPlugin { } // Create a separate entry for polyfills - assetMap.pages['/_polyfills'] = polyfillFiles + assetMap.polyfillFiles = polyfillFiles // Add the runtime build manifest file (generated later in this file) // as a dependency for the app. If the flag is false, the file won't be diff --git a/packages/next/export/index.ts b/packages/next/export/index.ts index 5105fda2317db..17c503f89ac67 100644 --- a/packages/next/export/index.ts +++ b/packages/next/export/index.ts @@ -248,7 +248,6 @@ export default async function exportApp( assetPrefix: nextConfig.assetPrefix.replace(/\/$/, ''), distDir, dev: false, - staticMarkup: false, hotReloader: null, basePath: nextConfig.experimental.basePath, canonicalBase: nextConfig.amp?.canonicalBase || '', diff --git a/packages/next/next-server/lib/utils.ts b/packages/next/next-server/lib/utils.ts index 87dbfa0772b6a..45f18c7ebae76 100644 --- a/packages/next/next-server/lib/utils.ts +++ b/packages/next/next-server/lib/utils.ts @@ -5,6 +5,7 @@ import { format, URLFormatOptions, UrlObject } from 'url' import { ManifestItem } from '../server/load-components' import { NextRouter } from './router/router' import { Env } from '../../lib/load-env-config' +import { BuildManifest } from '../server/get-page-files' /** * Types used by both next and next-server @@ -153,15 +154,12 @@ export type DocumentInitialProps = RenderPageResult & { export type DocumentProps = DocumentInitialProps & { __NEXT_DATA__: NEXT_DATA dangerousAsPath: string + buildManifest: BuildManifest ampPath: string inAmpMode: boolean hybridAmp: boolean - staticMarkup: boolean isDevelopment: boolean - devFiles: string[] files: string[] - lowPriorityFiles: string[] - polyfillFiles: string[] dynamicImports: ManifestItem[] assetPrefix?: string canonicalBase: string diff --git a/packages/next/next-server/server/get-page-files.ts b/packages/next/next-server/server/get-page-files.ts index ada704db2ecb7..92f9b4686988d 100644 --- a/packages/next/next-server/server/get-page-files.ts +++ b/packages/next/next-server/server/get-page-files.ts @@ -2,6 +2,7 @@ import { normalizePagePath } from './normalize-page-path' export type BuildManifest = { devFiles: string[] + polyfillFiles: string[] lowPriorityFiles: string[] pages: { '/_app': string[] diff --git a/packages/next/next-server/server/next-server.ts b/packages/next/next-server/server/next-server.ts index 1a347704cde13..7bbd08b10fca2 100644 --- a/packages/next/next-server/server/next-server.ts +++ b/packages/next/next-server/server/next-server.ts @@ -87,7 +87,6 @@ export type ServerConstructor = { * Where the Next project is located - @default '.' */ dir?: string - staticMarkup?: boolean /** * Hide error messages containing server information - @default false */ @@ -113,7 +112,6 @@ export default class Server { buildId: string renderOpts: { poweredByHeader: boolean - staticMarkup: boolean buildId: string generateEtags: boolean runtimeConfig?: { [key: string]: any } @@ -140,7 +138,6 @@ export default class Server { public constructor({ dir = '.', - staticMarkup = false, quiet = false, conf = null, dev = false, @@ -171,7 +168,6 @@ export default class Server { this.renderOpts = { poweredByHeader: this.nextConfig.poweredByHeader, canonicalBase: this.nextConfig.amp.canonicalBase, - staticMarkup, buildId: this.buildId, generateEtags, previewProps: this.getPreviewProps(), diff --git a/packages/next/next-server/server/render.tsx b/packages/next/next-server/server/render.tsx index c093511e6f588..0755f16f33210 100644 --- a/packages/next/next-server/server/render.tsx +++ b/packages/next/next-server/server/render.tsx @@ -119,7 +119,6 @@ function enhanceComponents( } function render( - renderElementToString: (element: React.ReactElement) => string, element: React.ReactElement, ampMode: any ): { html: string; head: React.ReactElement[] } { @@ -127,7 +126,7 @@ function render( let head try { - html = renderElementToString(element) + html = renderToString(element) } finally { head = Head.rewind() || defaultHead(isInAmpMode(ampMode)) } @@ -136,7 +135,6 @@ function render( } export type RenderOptsPartial = { - staticMarkup: boolean buildId: string canonicalBase: string runtimeConfig?: { [key: string]: any } @@ -165,6 +163,7 @@ export type RenderOpts = LoadComponentsReturnType & RenderOptsPartial function renderDocument( Document: DocumentType, { + buildManifest, props, docProps, pathname, @@ -184,11 +183,7 @@ function renderDocument( ampState, inAmpMode, hybridAmp, - staticMarkup, - devFiles, files, - lowPriorityFiles, - polyfillFiles, dynamicImports, htmlProps, bodyTags, @@ -211,10 +206,7 @@ function renderDocument( hybridAmp: boolean dynamicImportsIds: string[] dynamicImports: ManifestItem[] - devFiles: string[] files: string[] - lowPriorityFiles: string[] - polyfillFiles: string[] htmlProps: any bodyTags: any headTags: any @@ -250,17 +242,14 @@ function renderDocument( gip, // whether the page has getInitialProps appGip, // whether the _app has getInitialProps }, + buildManifest, dangerousAsPath, canonicalBase, ampPath, inAmpMode, isDevelopment: !!dev, hybridAmp, - staticMarkup, - devFiles, files, - lowPriorityFiles, - polyfillFiles, dynamicImports, assetPrefix, htmlProps, @@ -294,7 +283,6 @@ export async function renderToHTML( const { err, dev = false, - staticMarkup = false, ampPath = '', App, Document, @@ -677,27 +665,16 @@ export async function renderToHTML( // the response might be finished on the getInitialProps call if (isResSent(res) && !isSSG) return null - const devFiles = buildManifest.devFiles const files = [ ...new Set([ ...getPageFiles(buildManifest, '/_app'), ...getPageFiles(buildManifest, pathname), ]), ] - const lowPriorityFiles = buildManifest.lowPriorityFiles - const polyfillFiles = getPageFiles(buildManifest, '/_polyfills') - - const renderElementToString = staticMarkup - ? renderToStaticMarkup - : renderToString const renderPageError = (): { html: string; head: any } | void => { if (ctx.err && ErrorDebug) { - return render( - renderElementToString, - , - ampState - ) + return render(, ampState) } if (dev && (props.router || props.Component)) { @@ -719,7 +696,6 @@ export async function renderToHTML( } = enhanceComponents(options, App, Component) return render( - renderElementToString, , @@ -785,10 +761,7 @@ export async function renderToHTML( hybridAmp, dynamicImportsIds, dynamicImports, - devFiles, files, - lowPriorityFiles, - polyfillFiles, gsp: !!getStaticProps ? true : undefined, gssp: !!getServerSideProps ? true : undefined, gip: hasPageGetInitialProps ? true : undefined, diff --git a/packages/next/pages/_document.tsx b/packages/next/pages/_document.tsx index 8eca7149aea48..741c85dbddbc6 100644 --- a/packages/next/pages/_document.tsx +++ b/packages/next/pages/_document.tsx @@ -573,11 +573,11 @@ export class NextScript extends Component { } getScripts() { - const { assetPrefix, files, lowPriorityFiles } = this.context._documentProps + const { assetPrefix, files, buildManifest } = this.context._documentProps const { _devOnlyInvalidateCacheQueryString } = this.context const normalScripts = files?.filter((file) => file.endsWith('.js')) - const lowPriorityScripts = lowPriorityFiles?.filter((file) => + const lowPriorityScripts = buildManifest.lowPriorityFiles?.filter((file) => file.endsWith('.js') ) @@ -606,10 +606,10 @@ export class NextScript extends Component { getPolyfillScripts() { // polyfills.js has to be rendered as nomodule without async // It also has to be the first script to load - const { assetPrefix, polyfillFiles } = this.context._documentProps + const { assetPrefix, buildManifest } = this.context._documentProps const { _devOnlyInvalidateCacheQueryString } = this.context - return polyfillFiles + return buildManifest.polyfillFiles .filter( (polyfill) => polyfill.endsWith('.js') && !/\.module\.js$/.test(polyfill) @@ -642,10 +642,9 @@ export class NextScript extends Component { render() { const { - staticMarkup, assetPrefix, inAmpMode, - devFiles, + buildManifest, __NEXT_DATA__, bodyTags, unstable_runtimeJS, @@ -667,7 +666,7 @@ export class NextScript extends Component { return ( <> - {staticMarkup || disableRuntimeJS ? null : ( + {disableRuntimeJS ? null : (