diff --git a/packages/next/build/webpack-config.ts b/packages/next/build/webpack-config.ts index 2785088196320..28a6dbb3d9ae9 100644 --- a/packages/next/build/webpack-config.ts +++ b/packages/next/build/webpack-config.ts @@ -244,7 +244,17 @@ export function getDefineEnv({ 'process.env.__NEXT_BUILD_INDICATOR_POSITION': JSON.stringify( config.devIndicators.buildActivityPosition ), - 'process.env.__NEXT_STRICT_MODE': JSON.stringify(config.reactStrictMode), + 'process.env.__NEXT_STRICT_MODE': JSON.stringify( + config.reactStrictMode === null ? false : config.reactStrictMode + ), + 'process.env.__NEXT_STRICT_MODE_APP': JSON.stringify( + // When next.config.js does not have reactStrictMode enabling appDir will enable it. + config.reactStrictMode === null + ? config.experimental.appDir + ? true + : false + : config.reactStrictMode + ), 'process.env.__NEXT_REACT_ROOT': JSON.stringify(hasReactRoot), 'process.env.__NEXT_OPTIMIZE_FONTS': JSON.stringify( !dev && config.optimizeFonts diff --git a/packages/next/client/app-index.tsx b/packages/next/client/app-index.tsx index fe45a3eacb350..cc9c738828064 100644 --- a/packages/next/client/app-index.tsx +++ b/packages/next/client/app-index.tsx @@ -149,6 +149,10 @@ function ServerRoot({ cacheKey }: { cacheKey: string }): JSX.Element { return root } +const StrictModeIfEnabled = process.env.__NEXT_STRICT_MODE_APP + ? React.StrictMode + : React.Fragment + function Root({ children }: React.PropsWithChildren<{}>): React.ReactElement { React.useEffect(() => { measureWebVitals() @@ -213,7 +217,7 @@ export function hydrate() { } const reactEl = ( - + - + ) const isError = document.documentElement.id === '__next_error__' diff --git a/packages/next/server/config-shared.ts b/packages/next/server/config-shared.ts index 8b46186c766de..957ab2b69119a 100644 --- a/packages/next/server/config-shared.ts +++ b/packages/next/server/config-shared.ts @@ -391,7 +391,7 @@ export interface NextConfig extends Record { * * @see [React Strict Mode](https://nextjs.org/docs/api-reference/next.config.js/react-strict-mode) */ - reactStrictMode?: boolean + reactStrictMode?: boolean | null /** * Add public (in browser) runtime configuration to your app @@ -545,7 +545,7 @@ export const defaultConfig: NextConfig = { excludeDefaultMomentLocales: true, serverRuntimeConfig: {}, publicRuntimeConfig: {}, - reactStrictMode: false, + reactStrictMode: null, httpAgentOptions: { keepAlive: true, },