Unverified Commit 65083f8f authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Allow disabling Strict mode in app (#41894)

Follows `reactStrictMode`. Still recommended to have it enabled (which is the default for new apps).  This will become increasingly more important as other features on top of concurrent rendering are implemented.


## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent 9fe18e88
......@@ -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
......
......@@ -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 = (
<React.StrictMode>
<StrictModeIfEnabled>
<HeadManagerContext.Provider
value={{
appDir: true,
......@@ -223,7 +227,7 @@ export function hydrate() {
<RSCComponent />
</Root>
</HeadManagerContext.Provider>
</React.StrictMode>
</StrictModeIfEnabled>
)
const isError = document.documentElement.id === '__next_error__'
......
......@@ -391,7 +391,7 @@ export interface NextConfig extends Record<string, any> {
*
* @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,
},
......
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