Skip to content

Commit

Permalink
Clean up render.tsx options (vercel#13759)
Browse files Browse the repository at this point in the history
Went through and removed a bunch of internal options which are just pass-through values of buildManifest

Closes vercel#13851
  • Loading branch information
timneutkens authored and rokinsky committed Jul 11, 2020
1 parent d7f8472 commit 28dc5a2
Show file tree
Hide file tree
Showing 18 changed files with 91 additions and 178 deletions.
2 changes: 0 additions & 2 deletions packages/next/build/plugins/collect-plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ export type PluginMetaData = {
// currently supported middleware
export const VALID_MIDDLEWARE = [
'document-head-tags-server',
'document-body-tags-server',
'document-html-props-server',
'on-init-client',
'on-init-server',
'on-error-server',
Expand Down
2 changes: 1 addition & 1 deletion packages/next/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@ export default async function getBaseWebpackConfig(
}
}, {}),
'process.env.NODE_ENV': JSON.stringify(webpackMode),
'process.crossOrigin': JSON.stringify(crossOrigin),
'process.env.__NEXT_CROSS_ORIGIN': JSON.stringify(crossOrigin),
'process.browser': JSON.stringify(!isServer),
'process.env.__NEXT_TEST_MODE': JSON.stringify(
process.env.__NEXT_TEST_MODE
Expand Down
3 changes: 2 additions & 1 deletion packages/next/build/webpack/plugins/build-manifest-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export default class BuildManifestPlugin {
(compilation, callback) => {
const { chunks } = compilation
const assetMap: BuildManifest = {
polyfillFiles: [],
devFiles: [],
lowPriorityFiles: [],
pages: { '/_app': [] },
Expand Down Expand Up @@ -115,7 +116,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
Expand Down
16 changes: 1 addition & 15 deletions packages/next/build/webpack/plugins/react-loadable-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,16 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWAR
// Implementation of this PR: https://github.com/jamiebuilds/react-loadable/pull/132
// Modified to strip out unneeded results for Next's specific use case

import url from 'url'

import {
Compiler,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
compilation as CompilationType,
} from 'webpack'

function buildManifest(
compiler: Compiler,
_compiler: Compiler,
compilation: CompilationType.Compilation
) {
let context = compiler.options.context
let manifest: { [k: string]: any[] } = {}

compilation.chunkGroups.forEach((chunkGroup) => {
Expand All @@ -50,17 +47,8 @@ function buildManifest(
return
}

let publicPath = url.resolve(
compilation.outputOptions.publicPath || '',
file
)

for (const module of chunk.modulesIterable) {
let id = module.id
let name =
typeof module.libIdent === 'function'
? module.libIdent({ context })
: null

if (!manifest[request]) {
manifest[request] = []
Expand All @@ -77,9 +65,7 @@ function buildManifest(

manifest[request].push({
id,
name,
file,
publicPath,
})
}
})
Expand Down
4 changes: 2 additions & 2 deletions packages/next/client/page-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function getAssetPath(route) {
function appendLink(href, rel, as) {
return new Promise((res, rej, link) => {
link = document.createElement('link')
link.crossOrigin = process.crossOrigin
link.crossOrigin = process.env.__NEXT_CROSS_ORIGIN
link.href = href
link.rel = rel
if (as) link.as = as
Expand Down Expand Up @@ -267,7 +267,7 @@ export default class PageLoader {
// dependencies already have it added during build manifest creation
if (isPage) url = url.replace(/\.js$/, '.module.js')
}
script.crossOrigin = process.crossOrigin
script.crossOrigin = process.env.__NEXT_CROSS_ORIGIN
script.src = url
script.onerror = () => {
const error = new Error(`Error loading script ${url}`)
Expand Down
1 change: 0 additions & 1 deletion packages/next/export/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,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 || '',
Expand Down
8 changes: 2 additions & 6 deletions packages/next/next-server/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -153,20 +154,15 @@ 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
htmlProps: any
bodyTags: any[]
headTags: any[]
unstable_runtimeJS?: false
}
Expand Down
1 change: 1 addition & 0 deletions packages/next/next-server/server/get-page-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { normalizePagePath, denormalizePagePath } from './normalize-page-path'

export type BuildManifest = {
devFiles: string[]
polyfillFiles: string[]
lowPriorityFiles: string[]
pages: {
'/_app': string[]
Expand Down
1 change: 0 additions & 1 deletion packages/next/next-server/server/load-components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export type ManifestItem = {
id: number | string
name: string
file: string
publicPath: string
}

type ReactLoadableManifest = { [moduleId: string]: ManifestItem[] }
Expand Down
4 changes: 0 additions & 4 deletions packages/next/next-server/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -113,7 +112,6 @@ export default class Server {
buildId: string
renderOpts: {
poweredByHeader: boolean
staticMarkup: boolean
buildId: string
generateEtags: boolean
runtimeConfig?: { [key: string]: any }
Expand All @@ -140,7 +138,6 @@ export default class Server {

public constructor({
dir = '.',
staticMarkup = false,
quiet = false,
conf = null,
dev = false,
Expand Down Expand Up @@ -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(),
Expand Down
45 changes: 4 additions & 41 deletions packages/next/next-server/server/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,14 @@ function enhanceComponents(
}

function render(
renderElementToString: (element: React.ReactElement<any>) => string,
element: React.ReactElement<any>,
ampMode: any
): { html: string; head: React.ReactElement[] } {
let html
let head

try {
html = renderElementToString(element)
html = renderToString(element)
} finally {
head = Head.rewind() || defaultHead(isInAmpMode(ampMode))
}
Expand All @@ -136,7 +135,6 @@ function render(
}

export type RenderOptsPartial = {
staticMarkup: boolean
buildId: string
canonicalBase: string
runtimeConfig?: { [key: string]: any }
Expand Down Expand Up @@ -165,6 +163,7 @@ export type RenderOpts = LoadComponentsReturnType & RenderOptsPartial
function renderDocument(
Document: DocumentType,
{
buildManifest,
props,
docProps,
pathname,
Expand All @@ -184,14 +183,8 @@ function renderDocument(
ampState,
inAmpMode,
hybridAmp,
staticMarkup,
devFiles,
files,
lowPriorityFiles,
polyfillFiles,
dynamicImports,
htmlProps,
bodyTags,
headTags,
gsp,
gssp,
Expand All @@ -211,12 +204,7 @@ function renderDocument(
hybridAmp: boolean
dynamicImportsIds: string[]
dynamicImports: ManifestItem[]
devFiles: string[]
files: string[]
lowPriorityFiles: string[]
polyfillFiles: string[]
htmlProps: any
bodyTags: any
headTags: any
isFallback?: boolean
gsp?: boolean
Expand Down Expand Up @@ -250,21 +238,16 @@ 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,
bodyTags,
headTags,
unstable_runtimeJS,
...docProps,
Expand Down Expand Up @@ -293,7 +276,6 @@ export async function renderToHTML(
const {
err,
dev = false,
staticMarkup = false,
ampPath = '',
App,
Document,
Expand Down Expand Up @@ -334,8 +316,6 @@ export async function renderToHTML(
}

const headTags = (...args: any) => callMiddleware('headTags', args)
const bodyTags = (...args: any) => callMiddleware('bodyTags', args)
const htmlProps = (...args: any) => callMiddleware('htmlProps', args, true)

const didRewrite = (req as any)._nextDidRewrite
const isFallback = !!query.__nextFallback
Expand Down Expand Up @@ -674,27 +654,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,
<ErrorDebug error={ctx.err} />,
ampState
)
return render(<ErrorDebug error={ctx.err} />, ampState)
}

if (dev && (props.router || props.Component)) {
Expand All @@ -716,7 +685,6 @@ export async function renderToHTML(
} = enhanceComponents(options, App, Component)

return render(
renderElementToString,
<AppContainer>
<EnhancedApp Component={EnhancedComponent} router={router} {...props} />
</AppContainer>,
Expand Down Expand Up @@ -771,8 +739,6 @@ export async function renderToHTML(
ampState,
props,
headTags: await headTags(documentCtx),
bodyTags: await bodyTags(documentCtx),
htmlProps: await htmlProps(documentCtx),
isFallback,
docProps,
pathname,
Expand All @@ -782,10 +748,7 @@ export async function renderToHTML(
hybridAmp,
dynamicImportsIds,
dynamicImports,
devFiles,
files,
lowPriorityFiles,
polyfillFiles,
gsp: !!getStaticProps ? true : undefined,
gssp: !!getServerSideProps ? true : undefined,
gip: hasPageGetInitialProps ? true : undefined,
Expand Down
Loading

0 comments on commit 28dc5a2

Please sign in to comment.