Skip to content

Commit 65083f8

Browse files
authored
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)
1 parent 9fe18e8 commit 65083f8

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

packages/next/build/webpack-config.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,17 @@ export function getDefineEnv({
244244
'process.env.__NEXT_BUILD_INDICATOR_POSITION': JSON.stringify(
245245
config.devIndicators.buildActivityPosition
246246
),
247-
'process.env.__NEXT_STRICT_MODE': JSON.stringify(config.reactStrictMode),
247+
'process.env.__NEXT_STRICT_MODE': JSON.stringify(
248+
config.reactStrictMode === null ? false : config.reactStrictMode
249+
),
250+
'process.env.__NEXT_STRICT_MODE_APP': JSON.stringify(
251+
// When next.config.js does not have reactStrictMode enabling appDir will enable it.
252+
config.reactStrictMode === null
253+
? config.experimental.appDir
254+
? true
255+
: false
256+
: config.reactStrictMode
257+
),
248258
'process.env.__NEXT_REACT_ROOT': JSON.stringify(hasReactRoot),
249259
'process.env.__NEXT_OPTIMIZE_FONTS': JSON.stringify(
250260
!dev && config.optimizeFonts

packages/next/client/app-index.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ function ServerRoot({ cacheKey }: { cacheKey: string }): JSX.Element {
149149
return root
150150
}
151151

152+
const StrictModeIfEnabled = process.env.__NEXT_STRICT_MODE_APP
153+
? React.StrictMode
154+
: React.Fragment
155+
152156
function Root({ children }: React.PropsWithChildren<{}>): React.ReactElement {
153157
React.useEffect(() => {
154158
measureWebVitals()
@@ -213,7 +217,7 @@ export function hydrate() {
213217
}
214218

215219
const reactEl = (
216-
<React.StrictMode>
220+
<StrictModeIfEnabled>
217221
<HeadManagerContext.Provider
218222
value={{
219223
appDir: true,
@@ -223,7 +227,7 @@ export function hydrate() {
223227
<RSCComponent />
224228
</Root>
225229
</HeadManagerContext.Provider>
226-
</React.StrictMode>
230+
</StrictModeIfEnabled>
227231
)
228232

229233
const isError = document.documentElement.id === '__next_error__'

packages/next/server/config-shared.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ export interface NextConfig extends Record<string, any> {
391391
*
392392
* @see [React Strict Mode](https://nextjs.org/docs/api-reference/next.config.js/react-strict-mode)
393393
*/
394-
reactStrictMode?: boolean
394+
reactStrictMode?: boolean | null
395395

396396
/**
397397
* Add public (in browser) runtime configuration to your app
@@ -545,7 +545,7 @@ export const defaultConfig: NextConfig = {
545545
excludeDefaultMomentLocales: true,
546546
serverRuntimeConfig: {},
547547
publicRuntimeConfig: {},
548-
reactStrictMode: false,
548+
reactStrictMode: null,
549549
httpAgentOptions: {
550550
keepAlive: true,
551551
},

0 commit comments

Comments
 (0)