Skip to content

Commit

Permalink
Revert "Make concurrent features independent from the global runtime …
Browse files Browse the repository at this point in the history
…option (#35245)"

This reverts commit 853442d.
  • Loading branch information
shuding committed Mar 17, 2022
1 parent 52e34ce commit 201c7cf
Show file tree
Hide file tree
Showing 40 changed files with 60 additions and 468 deletions.
3 changes: 3 additions & 0 deletions packages/next/build/entries.ts
Expand Up @@ -187,6 +187,9 @@ export async function getPageRuntime(
if (!pageRuntime) {
if (isRuntimeRequired) {
pageRuntime = globalRuntimeFallback
} else {
// @TODO: Remove this branch to fully implement the RFC.
pageRuntime = globalRuntimeFallback
}
}

Expand Down
35 changes: 10 additions & 25 deletions packages/next/build/index.ts
Expand Up @@ -76,11 +76,7 @@ import {
} from '../telemetry/events'
import { Telemetry } from '../telemetry/storage'
import { CompilerResult, runCompiler } from './compiler'
import {
createEntrypoints,
createPagesMapping,
getPageRuntime,
} from './entries'
import { createEntrypoints, createPagesMapping } from './entries'
import { generateBuildId } from './generate-build-id'
import { isWriteable } from './is-writeable'
import * as Log from './output/log'
Expand Down Expand Up @@ -157,10 +153,11 @@ export default async function build(
setGlobal('phase', PHASE_PRODUCTION_BUILD)
setGlobal('distDir', distDir)

// We enable concurrent features (Fizz-related rendering architecture) when
// using React 18 or experimental.
// Currently, when the runtime option is set (either `nodejs` or `edge`),
// we enable concurrent features (Fizz-related rendering architecture).
const runtime = config.experimental.runtime
const hasReactRoot = shouldUseReactRoot()
const hasConcurrentFeatures = hasReactRoot
const hasConcurrentFeatures = !!runtime

const hasServerComponents =
hasReactRoot && !!config.experimental.serverComponents
Expand Down Expand Up @@ -625,7 +622,6 @@ export default async function build(
entrypoints: entrypoints.client,
rewrites,
runWebpackSpan,
hasReactRoot,
}),
getBaseWebpackConfig(dir, {
buildId,
Expand All @@ -637,7 +633,6 @@ export default async function build(
entrypoints: entrypoints.server,
rewrites,
runWebpackSpan,
hasReactRoot,
}),
hasReactRoot
? getBaseWebpackConfig(dir, {
Expand All @@ -651,7 +646,6 @@ export default async function build(
entrypoints: entrypoints.edgeServer,
rewrites,
runWebpackSpan,
hasReactRoot,
})
: null,
])
Expand Down Expand Up @@ -960,22 +954,10 @@ export default async function build(
let ssgPageRoutes: string[] | null = null
let isMiddlewareRoute = !!page.match(MIDDLEWARE_ROUTE)

const pagePath = pagePaths.find((_path) =>
_path.startsWith(actualPage + '.')
)
const pageRuntime =
hasConcurrentFeatures && pagePath
? await getPageRuntime(
join(pagesDir, pagePath),
config.experimental.runtime
)
: null

if (
!isMiddlewareRoute &&
!isReservedPage(page) &&
// We currently don't support staic optimization in the Edge runtime.
pageRuntime !== 'edge'
!hasConcurrentFeatures
) {
try {
let isPageStaticSpan =
Expand Down Expand Up @@ -1501,7 +1483,10 @@ export default async function build(

const combinedPages = [...staticPages, ...ssgPages]

if (combinedPages.length > 0 || useStatic404 || useDefaultStatic500) {
if (
!hasConcurrentFeatures &&
(combinedPages.length > 0 || useStatic404 || useDefaultStatic500)
) {
const staticGenerationSpan = nextBuildSpan.traceChild('static-generation')
await staticGenerationSpan.traceAsyncFn(async () => {
detectConflictingPaths(
Expand Down
13 changes: 6 additions & 7 deletions packages/next/build/webpack-config.ts
Expand Up @@ -48,6 +48,7 @@ import type { Span } from '../trace'
import { getRawPageExtensions } from './utils'
import browserslist from 'next/dist/compiled/browserslist'
import loadJsConfig from './load-jsconfig'
import { shouldUseReactRoot } from '../server/config'
import { getMiddlewareSourceMapPlugins } from './webpack/plugins/middleware-source-maps-plugin'

const watchOptions = Object.freeze({
Expand Down Expand Up @@ -309,7 +310,6 @@ export default async function getBaseWebpackConfig(
rewrites,
isDevFallback = false,
runWebpackSpan,
hasReactRoot,
}: {
buildId: string
config: NextConfigComplete
Expand All @@ -323,7 +323,6 @@ export default async function getBaseWebpackConfig(
rewrites: CustomRoutes['rewrites']
isDevFallback?: boolean
runWebpackSpan: Span
hasReactRoot: boolean
}
): Promise<webpack.Configuration> {
const { useTypeScript, jsConfig, resolvedBaseUrl } = await loadJsConfig(
Expand All @@ -336,10 +335,10 @@ export default async function getBaseWebpackConfig(
rewrites.afterFiles.length > 0 ||
rewrites.fallback.length > 0
const hasReactRefresh: boolean = dev && !isServer

const hasReactRoot = shouldUseReactRoot()
const runtime = config.experimental.runtime

// Make sure `reactRoot` is enabled when React 18 or experimental is detected.
// Make sure reactRoot is enabled when react 18 is detected
if (hasReactRoot) {
config.experimental.reactRoot = true
}
Expand All @@ -354,14 +353,14 @@ export default async function getBaseWebpackConfig(
'`experimental.runtime` requires `experimental.reactRoot` to be enabled along with React 18.'
)
}
if (config.experimental.serverComponents && !hasReactRoot) {
if (config.experimental.serverComponents && !runtime) {
throw new Error(
'`experimental.serverComponents` requires React 18 to be installed.'
'`experimental.runtime` is required to be set along with `experimental.serverComponents`.'
)
}

const targetWeb = isEdgeRuntime || !isServer
const hasConcurrentFeatures = hasReactRoot
const hasConcurrentFeatures = !!runtime && hasReactRoot
const hasServerComponents =
hasConcurrentFeatures && !!config.experimental.serverComponents
const disableOptimizedLoading = hasConcurrentFeatures
Expand Down
1 change: 0 additions & 1 deletion packages/next/export/index.ts
Expand Up @@ -588,7 +588,6 @@ export default async function exportApp(
nextConfig.experimental.disableOptimizedLoading,
parentSpanId: pageExportSpan.id,
httpAgentOptions: nextConfig.httpAgentOptions,
serverComponents: nextConfig.experimental.serverComponents,
})

for (const validation of result.ampValidations || []) {
Expand Down
11 changes: 2 additions & 9 deletions packages/next/export/worker.ts
Expand Up @@ -59,7 +59,6 @@ interface ExportPageInput {
disableOptimizedLoading: any
parentSpanId: any
httpAgentOptions: NextConfigComplete['httpAgentOptions']
serverComponents?: boolean
}

interface ExportPageResults {
Expand Down Expand Up @@ -107,7 +106,6 @@ export default async function exportPage({
optimizeCss,
disableOptimizedLoading,
httpAgentOptions,
serverComponents,
}: ExportPageInput): Promise<ExportPageResults> {
setHttpAgentOptions(httpAgentOptions)
const exportPageSpan = trace('export-page-worker', parentSpanId)
Expand Down Expand Up @@ -262,7 +260,7 @@ export default async function exportPage({
getServerSideProps,
getStaticProps,
pageConfig,
} = await loadComponents(distDir, page, serverless, serverComponents)
} = await loadComponents(distDir, page, serverless)
const ampState = {
ampFirst: pageConfig?.amp === true,
hasQuery: Boolean(query.amp),
Expand Down Expand Up @@ -323,12 +321,7 @@ export default async function exportPage({
throw new Error(`Failed to render serverless page`)
}
} else {
const components = await loadComponents(
distDir,
page,
serverless,
serverComponents
)
const components = await loadComponents(distDir, page, serverless)
const ampState = {
ampFirst: components.pageConfig?.amp === true,
hasQuery: Boolean(query.amp),
Expand Down
4 changes: 3 additions & 1 deletion packages/next/pages/_document.tsx
Expand Up @@ -585,9 +585,11 @@ export class Head extends Component<
disableOptimizedLoading,
optimizeCss,
optimizeFonts,
hasConcurrentFeatures,
runtime,
} = this.context

const hasConcurrentFeatures = !!runtime

const disableRuntimeJS = unstable_runtimeJS === false
const disableJsPreload =
unstable_JsPreload === false || !disableOptimizedLoading
Expand Down
13 changes: 4 additions & 9 deletions packages/next/server/dev/hot-reloader.ts
Expand Up @@ -154,7 +154,6 @@ export default class HotReloader {
private config: NextConfigComplete
private runtime?: 'nodejs' | 'edge'
private hasServerComponents: boolean
private hasReactRoot: boolean
public clientStats: webpack5.Stats | null
public serverStats: webpack5.Stats | null
private clientError: Error | null = null
Expand Down Expand Up @@ -198,9 +197,7 @@ export default class HotReloader {

this.config = config
this.runtime = config.experimental.runtime
this.hasReactRoot = shouldUseReactRoot()
this.hasServerComponents =
this.hasReactRoot && !!config.experimental.serverComponents
this.hasServerComponents = !!config.experimental.serverComponents
this.previewProps = previewProps
this.rewrites = rewrites
this.hotReloaderSpan = trace('hot-reloader', undefined, {
Expand Down Expand Up @@ -343,6 +340,8 @@ export default class HotReloader {
)
)

const hasReactRoot = shouldUseReactRoot()

return webpackConfigSpan
.traceChild('generate-webpack-config')
.traceAsyncFn(() =>
Expand All @@ -357,7 +356,6 @@ export default class HotReloader {
rewrites: this.rewrites,
entrypoints: entrypoints.client,
runWebpackSpan: this.hotReloaderSpan,
hasReactRoot: this.hasReactRoot,
}),
getBaseWebpackConfig(this.dir, {
dev: true,
Expand All @@ -368,10 +366,9 @@ export default class HotReloader {
rewrites: this.rewrites,
entrypoints: entrypoints.server,
runWebpackSpan: this.hotReloaderSpan,
hasReactRoot: this.hasReactRoot,
}),
// The edge runtime is only supported with React root.
this.hasReactRoot
hasReactRoot
? getBaseWebpackConfig(this.dir, {
dev: true,
isServer: true,
Expand All @@ -382,7 +379,6 @@ export default class HotReloader {
rewrites: this.rewrites,
entrypoints: entrypoints.edgeServer,
runWebpackSpan: this.hotReloaderSpan,
hasReactRoot: this.hasReactRoot,
})
: null,
].filter(Boolean) as webpack.Configuration[]
Expand Down Expand Up @@ -421,7 +417,6 @@ export default class HotReloader {
this.pagesDir
)
).client,
hasReactRoot: this.hasReactRoot,
})
const fallbackCompiler = webpack(fallbackConfig)

Expand Down
18 changes: 5 additions & 13 deletions packages/next/server/load-components.ts
Expand Up @@ -6,7 +6,6 @@ import type {
import {
BUILD_MANIFEST,
REACT_LOADABLE_MANIFEST,
MIDDLEWARE_FLIGHT_MANIFEST,
} from '../shared/lib/constants'
import { join } from 'path'
import { requirePage } from './require'
Expand All @@ -31,7 +30,6 @@ export type LoadComponentsReturnType = {
pageConfig: PageConfig
buildManifest: BuildManifest
reactLoadableManifest: ReactLoadableManifest
serverComponentManifest?: any | null
Document: DocumentType
App: AppType
getStaticProps?: GetStaticProps
Expand Down Expand Up @@ -63,8 +61,7 @@ export async function loadDefaultErrorComponents(distDir: string) {
export async function loadComponents(
distDir: string,
pathname: string,
serverless: boolean,
serverComponents?: boolean
serverless: boolean
): Promise<LoadComponentsReturnType> {
if (serverless) {
const ComponentMod = await requirePage(pathname, distDir, serverless)
Expand Down Expand Up @@ -105,14 +102,10 @@ export async function loadComponents(
requirePage(pathname, distDir, serverless),
])

const [buildManifest, reactLoadableManifest, serverComponentManifest] =
await Promise.all([
require(join(distDir, BUILD_MANIFEST)),
require(join(distDir, REACT_LOADABLE_MANIFEST)),
serverComponents
? require(join(distDir, 'server', MIDDLEWARE_FLIGHT_MANIFEST + '.json'))
: null,
])
const [buildManifest, reactLoadableManifest] = await Promise.all([
require(join(distDir, BUILD_MANIFEST)),
require(join(distDir, REACT_LOADABLE_MANIFEST)),
])

const Component = interopDefault(ComponentMod)
const Document = interopDefault(DocumentMod)
Expand All @@ -132,6 +125,5 @@ export async function loadComponents(
getServerSideProps,
getStaticProps,
getStaticPaths,
serverComponentManifest,
}
}
2 changes: 1 addition & 1 deletion packages/next/server/next-server.ts
Expand Up @@ -693,7 +693,7 @@ export default class NextNodeServer extends BaseServer {
}

protected getServerComponentManifest() {
if (!this.nextConfig.experimental.serverComponents) return undefined
if (!this.nextConfig.experimental.runtime) return undefined
return require(join(
this.distDir,
'server',
Expand Down
15 changes: 7 additions & 8 deletions packages/next/server/render.tsx
Expand Up @@ -450,12 +450,12 @@ export async function renderToHTML(
supportsDynamicHTML,
images,
reactRoot,
runtime: globalRuntime,
runtime,
ComponentMod,
AppMod,
} = renderOpts

const hasConcurrentFeatures = reactRoot
const hasConcurrentFeatures = !!runtime

let Document = renderOpts.Document
const OriginalComponent = renderOpts.Component
Expand All @@ -464,7 +464,7 @@ export async function renderToHTML(
const isServerComponent =
!!serverComponentManifest &&
hasConcurrentFeatures &&
!!ComponentMod.__next_rsc__
ComponentMod.__next_rsc__

let Component: React.ComponentType<{}> | ((props: any) => JSX.Element) =
renderOpts.Component
Expand Down Expand Up @@ -1243,7 +1243,7 @@ export async function renderToHTML(
| typeof Document
| undefined

if (process.browser && Document.getInitialProps) {
if (runtime === 'edge' && Document.getInitialProps) {
// In the Edge runtime, `Document.getInitialProps` isn't supported.
// We throw an error here if it's customized.
if (!builtinDocument) {
Expand Down Expand Up @@ -1329,8 +1329,7 @@ export async function renderToHTML(
) : (
<Body>
<AppContainerWithIsomorphicFiberStructure>
{isServerComponent && AppMod.__next_rsc__ ? (
// _app.server.js is used.
{renderOpts.serverComponents && AppMod.__next_rsc__ ? (
<Component {...props.pageProps} router={router} />
) : (
<App {...props} Component={Component} router={router} />
Expand Down Expand Up @@ -1362,6 +1361,7 @@ export async function renderToHTML(
),
generateStaticHTML: true,
})

const flushed = await streamToString(flushEffectStream)
return flushed
}
Expand Down Expand Up @@ -1489,8 +1489,7 @@ export async function renderToHTML(
optimizeCss: renderOpts.optimizeCss,
optimizeFonts: renderOpts.optimizeFonts,
nextScriptWorkers: renderOpts.nextScriptWorkers,
runtime: globalRuntime,
hasConcurrentFeatures,
runtime,
}

const document = (
Expand Down

0 comments on commit 201c7cf

Please sign in to comment.