Skip to content

Commit

Permalink
Clean up RenderOptsPartial (#23614)
Browse files Browse the repository at this point in the history
* Remove autoExport from RenderOpts

* Remove ampMode from RenderOpts

* Stop passing inAmpMode and hybridAmp

* Unset nextExport when falsy
  • Loading branch information
devknoll authored Apr 2, 2021
1 parent 95b9839 commit af3315b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
35 changes: 25 additions & 10 deletions packages/next/export/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { requireFontManifest } from '../next-server/server/require'
import { FontManifest } from '../next-server/server/font-utils'
import { normalizeLocalePath } from '../next-server/lib/i18n/normalize-locale-path'
import { trace } from '../telemetry/trace'
import { isInAmpMode } from '../next-server/lib/amp'

const envConfig = require('../next-server/lib/runtime-config')

Expand Down Expand Up @@ -67,8 +68,6 @@ interface RenderOpts {
ampPath?: string
ampValidatorPath?: string
ampSkipValidation?: boolean
hybridAmp?: boolean
inAmpMode?: boolean
optimizeFonts?: boolean
optimizeImages?: boolean
optimizeCss?: any
Expand Down Expand Up @@ -215,6 +214,8 @@ export default async function exportPage({
let html
let curRenderOpts: RenderOpts = {}
let renderMethod = renderToHTML
let inAmpMode = false,
hybridAmp = false

const renderedDuringBuild = (getStaticProps: any) => {
return !buildExport && getStaticProps && !isDynamicRoute(path)
Expand All @@ -229,11 +230,18 @@ export default async function exportPage({
...query,
},
})
const { Component: mod, getServerSideProps } = await loadComponents(
distDir,
page,
serverless
)
const {
Component: mod,
getServerSideProps,
pageConfig,
} = await loadComponents(distDir, page, serverless)
const ampState = {
ampFirst: pageConfig?.amp === true,
hasQuery: Boolean(query.amp),
hybrid: pageConfig?.amp === 'hybrid',
}
inAmpMode = isInAmpMode(ampState)
hybridAmp = ampState.hybrid

if (getServerSideProps) {
throw new Error(
Expand Down Expand Up @@ -292,6 +300,13 @@ export default async function exportPage({
}
} else {
const components = await loadComponents(distDir, page, serverless)
const ampState = {
ampFirst: components.pageConfig?.amp === true,
hasQuery: Boolean(query.amp),
hybrid: components.pageConfig?.amp === 'hybrid',
}
inAmpMode = isInAmpMode(ampState)
hybridAmp = ampState.hybrid

if (components.getServerSideProps) {
throw new Error(
Expand Down Expand Up @@ -371,11 +386,11 @@ export default async function exportPage({
}
}

if (curRenderOpts.inAmpMode && !curRenderOpts.ampSkipValidation) {
if (inAmpMode && !curRenderOpts.ampSkipValidation) {
if (!results.ssgNotFound) {
await validateAmp(html, path, curRenderOpts.ampValidatorPath)
}
} else if (curRenderOpts.hybridAmp) {
} else if (hybridAmp) {
// we need to render the AMP version
let ampHtmlFilename = `${ampPath}${sep}index.html`
if (!subFolders) {
Expand Down Expand Up @@ -433,7 +448,7 @@ export default async function exportPage({
'utf8'
)

if (curRenderOpts.hybridAmp) {
if (hybridAmp) {
await promises.writeFile(
dataFile.replace(/\.json$/, '.amp.json'),
JSON.stringify((curRenderOpts as any).pageData),
Expand Down
16 changes: 5 additions & 11 deletions packages/next/next-server/server/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,9 @@ export type RenderOptsPartial = {
runtimeConfig?: { [key: string]: any }
assetPrefix?: string
err?: Error | null
autoExport?: boolean
nextExport?: boolean
dev?: boolean
ampMode?: any
ampPath?: string
inAmpMode?: boolean
hybridAmp?: boolean
ErrorDebug?: React.ComponentType<{ error: Error }>
ampValidator?: (html: string, pathname: string) => Promise<void>
ampSkipValidation?: boolean
Expand Down Expand Up @@ -261,6 +257,7 @@ function renderDocument(
devOnlyCacheBusterQueryString: string
scriptLoader: any
isPreview?: boolean
autoExport?: boolean
}
): string {
return (
Expand Down Expand Up @@ -533,7 +530,6 @@ export async function renderToHTML(
req.url!.endsWith('/') && pathname !== '/' && !pageIsDynamic ? '/' : ''
}`
req.url = pathname
renderOpts.nextExport = true
}

if (pathname === '/404' && (hasPageGetInitialProps || getServerSideProps)) {
Expand All @@ -550,8 +546,6 @@ export async function renderToHTML(
)
}
}
if (isAutoExport) renderOpts.autoExport = true
if (isSSG) renderOpts.nextExport = false

await Loadable.preloadAll() // Make sure all dynamic imports are loaded

Expand Down Expand Up @@ -1038,11 +1032,9 @@ export async function renderToHTML(
const dynamicImportsIds = [...dynamicImportIdsSet]
const hybridAmp = ampState.hybrid

// update renderOpts so export knows current state
renderOpts.inAmpMode = inAmpMode
renderOpts.hybridAmp = hybridAmp

const docComponentsRendered: DocumentProps['docComponentsRendered'] = {}
const nextExport =
!isSSG && (renderOpts.nextExport || (dev && (isAutoExport || isFallback)))

let html = renderDocument(Document, {
...renderOpts,
Expand Down Expand Up @@ -1078,6 +1070,8 @@ export async function renderToHTML(
devOnlyCacheBusterQueryString,
scriptLoader,
isPreview: isPreview === true ? true : undefined,
autoExport: isAutoExport === true ? true : undefined,
nextExport: nextExport === true ? true : undefined,
})

if (process.env.NODE_ENV !== 'production') {
Expand Down

0 comments on commit af3315b

Please sign in to comment.