Unverified Commit 29402f3c authored by Gerald Monaco's avatar Gerald Monaco Committed by GitHub

Replace experimental `reactMode` with `reactRoot` (#24280)

* Remove reactMode config

* Add config + warning

* Tweak wording on warning

* Tweak wording on warning

* Don't default reactRoot for 10.x

* yak shaving

* Add warning when using old reactMode
parent 2743a74b
...@@ -29,9 +29,9 @@ jobs: ...@@ -29,9 +29,9 @@ jobs:
# needs: build # needs: build
env: env:
NEXT_TELEMETRY_DISABLED: 1 NEXT_TELEMETRY_DISABLED: 1
NEXT_PRIVATE_REACT_MODE: concurrent
HEADLESS: true HEADLESS: true
NEXT_PRIVATE_SKIP_SIZE_TESTS: true NEXT_PRIVATE_SKIP_SIZE_TESTS: true
NEXT_PRIVATE_REACT_ROOT: 1
strategy: strategy:
fail-fast: false fail-fast: false
matrix: matrix:
......
...@@ -1048,8 +1048,8 @@ export default async function getBaseWebpackConfig( ...@@ -1048,8 +1048,8 @@ export default async function getBaseWebpackConfig(
'process.env.__NEXT_STRICT_MODE': JSON.stringify( 'process.env.__NEXT_STRICT_MODE': JSON.stringify(
config.reactStrictMode config.reactStrictMode
), ),
'process.env.__NEXT_REACT_MODE': JSON.stringify( 'process.env.__NEXT_REACT_ROOT': JSON.stringify(
config.experimental.reactMode config.experimental.reactRoot
), ),
'process.env.__NEXT_OPTIMIZE_FONTS': JSON.stringify( 'process.env.__NEXT_OPTIMIZE_FONTS': JSON.stringify(
config.optimizeFonts && !dev config.optimizeFonts && !dev
......
...@@ -494,7 +494,8 @@ export function renderError(renderErrorProps: RenderErrorProps): Promise<any> { ...@@ -494,7 +494,8 @@ export function renderError(renderErrorProps: RenderErrorProps): Promise<any> {
} }
let reactRoot: any = null let reactRoot: any = null
let shouldUseHydrate: boolean = typeof ReactDOM.hydrate === 'function' let shouldHydrate: boolean = typeof ReactDOM.hydrate === 'function'
function renderReactElement( function renderReactElement(
domEl: HTMLElement, domEl: HTMLElement,
fn: (cb: () => void) => JSX.Element fn: (cb: () => void) => JSX.Element
...@@ -504,24 +505,24 @@ function renderReactElement( ...@@ -504,24 +505,24 @@ function renderReactElement(
performance.mark('beforeRender') performance.mark('beforeRender')
} }
const reactEl = fn( const reactEl = fn(shouldHydrate ? markHydrateComplete : markRenderComplete)
shouldUseHydrate ? markHydrateComplete : markRenderComplete if (process.env.__NEXT_REACT_ROOT) {
)
if (process.env.__NEXT_REACT_MODE !== 'legacy') {
if (!reactRoot) { if (!reactRoot) {
const opts = { hydrate: shouldUseHydrate } const createRootName =
reactRoot = typeof (ReactDOM as any).unstable_createRoot === 'function'
process.env.__NEXT_REACT_MODE === 'concurrent' ? 'unstable_createRoot'
? (ReactDOM as any).unstable_createRoot(domEl, opts) : 'createRoot'
: (ReactDOM as any).unstable_createBlockingRoot(domEl, opts) reactRoot = (ReactDOM as any)[createRootName](domEl, {
hydrate: shouldHydrate,
})
} }
reactRoot.render(reactEl) reactRoot.render(reactEl)
shouldUseHydrate = false shouldHydrate = false
} else { } else {
// The check for `.hydrate` is there to support React alternatives like preact // The check for `.hydrate` is there to support React alternatives like preact
if (shouldUseHydrate) { if (shouldHydrate) {
ReactDOM.hydrate(reactEl, domEl) ReactDOM.hydrate(reactEl, domEl)
shouldUseHydrate = false shouldHydrate = false
} else { } else {
ReactDOM.render(reactEl, domEl) ReactDOM.render(reactEl, domEl)
} }
......
...@@ -60,6 +60,7 @@ export type NextConfig = { [key: string]: any } & { ...@@ -60,6 +60,7 @@ export type NextConfig = { [key: string]: any } & {
skipValidation?: boolean skipValidation?: boolean
} }
turboMode: boolean turboMode: boolean
reactRoot: boolean
} }
} }
...@@ -104,7 +105,6 @@ export const defaultConfig: NextConfig = { ...@@ -104,7 +105,6 @@ export const defaultConfig: NextConfig = {
plugins: false, plugins: false,
profiling: false, profiling: false,
sprFlushToDisk: true, sprFlushToDisk: true,
reactMode: (process.env.NEXT_PRIVATE_REACT_MODE as any) || 'legacy',
workerThreads: false, workerThreads: false,
pageEnv: false, pageEnv: false,
optimizeImages: false, optimizeImages: false,
...@@ -115,6 +115,7 @@ export const defaultConfig: NextConfig = { ...@@ -115,6 +115,7 @@ export const defaultConfig: NextConfig = {
externalDir: false, externalDir: false,
serialWebpackBuild: false, serialWebpackBuild: false,
turboMode: false, turboMode: false,
reactRoot: Number(process.env.NEXT_PRIVATE_REACT_ROOT) > 0,
}, },
future: { future: {
strictPostcssConfiguration: false, strictPostcssConfiguration: false,
......
...@@ -13,7 +13,6 @@ import { loadEnvConfig } from '@next/env' ...@@ -13,7 +13,6 @@ import { loadEnvConfig } from '@next/env'
export { DomainLocales, NextConfig, normalizeConfig } from './config-shared' export { DomainLocales, NextConfig, normalizeConfig } from './config-shared'
const targets = ['server', 'serverless', 'experimental-serverless-trace'] const targets = ['server', 'serverless', 'experimental-serverless-trace']
const reactModes = ['legacy', 'blocking', 'concurrent']
const experimentalWarning = execOnce(() => { const experimentalWarning = execOnce(() => {
Log.warn(chalk.bold('You have enabled experimental feature(s).')) Log.warn(chalk.bold('You have enabled experimental feature(s).'))
...@@ -36,6 +35,19 @@ function assignDefaults(userConfig: { [key: string]: any }) { ...@@ -36,6 +35,19 @@ function assignDefaults(userConfig: { [key: string]: any }) {
delete userConfig.exportTrailingSlash delete userConfig.exportTrailingSlash
} }
if (typeof userConfig.experimental?.reactMode !== 'undefined') {
console.warn(
chalk.yellow.bold('Warning: ') +
'The experimental "reactMode" option has been replaced with "reactRoot". Please update your next.config.js.'
)
if (typeof userConfig.experimental?.reactRoot === 'undefined') {
userConfig.experimental.reactRoot = ['concurrent', 'blocking'].includes(
userConfig.experimental.reactMode
)
}
delete userConfig.experimental.reactMode
}
const config = Object.keys(userConfig).reduce<{ [key: string]: any }>( const config = Object.keys(userConfig).reduce<{ [key: string]: any }>(
(currentConfig, key) => { (currentConfig, key) => {
const value = userConfig[key] const value = userConfig[key]
...@@ -435,17 +447,6 @@ export default async function loadConfig( ...@@ -435,17 +447,6 @@ export default async function loadConfig(
: canonicalBase) || '' : canonicalBase) || ''
} }
if (
userConfig.experimental?.reactMode &&
!reactModes.includes(userConfig.experimental.reactMode)
) {
throw new Error(
`Specified React Mode is invalid. Provided: ${
userConfig.experimental.reactMode
} should be one of ${reactModes.join(', ')}`
)
}
if (hasNextSupport) { if (hasNextSupport) {
userConfig.target = process.env.NEXT_PRIVATE_TARGET || 'server' userConfig.target = process.env.NEXT_PRIVATE_TARGET || 'server'
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment