Skip to content

Commit 5b57bc6

Browse files
authored
[Draft] don't patch console during first render (#22308)
Previously, DevTools always overrode the native console to dim or supress StrictMode double logging. It also overrode console.log (in addition to console.error and console.warn). However, this changes the location shown by the browser console, which causes a bad developer experience. There is currently a TC39 proposal that would allow us to extend console without breaking developer experience, but in the meantime this PR changes the StrictMode console override behavior so that we only patch the console during the StrictMode double render so that, during the first render, the location points to developer code rather than our DevTools console code.
1 parent cf07c3d commit 5b57bc6

18 files changed

+676
-454
lines changed

packages/react-devtools-shared/src/__tests__/console-test.js

+172-50
Large diffs are not rendered by default.

packages/react-devtools-shared/src/backend/console.js

+200-137
Large diffs are not rendered by default.

packages/react-devtools-shared/src/backend/legacy/renderer.js

+6
Original file line numberDiff line numberDiff line change
@@ -1073,6 +1073,10 @@ export function attach(
10731073
// Not implemented
10741074
}
10751075

1076+
function patchConsoleForStrictMode() {}
1077+
1078+
function unpatchConsoleForStrictMode() {}
1079+
10761080
return {
10771081
clearErrorsAndWarnings,
10781082
clearErrorsForFiberID,
@@ -1101,6 +1105,7 @@ export function attach(
11011105
overrideSuspense,
11021106
overrideValueAtPath,
11031107
renamePath,
1108+
patchConsoleForStrictMode,
11041109
prepareViewAttributeSource,
11051110
prepareViewElementSource,
11061111
renderer,
@@ -1109,6 +1114,7 @@ export function attach(
11091114
startProfiling,
11101115
stopProfiling,
11111116
storeAsGlobal,
1117+
unpatchConsoleForStrictMode,
11121118
updateComponentFilters,
11131119
};
11141120
}

packages/react-devtools-shared/src/backend/renderer.js

+4
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ import {inspectHooksOfFiber} from 'react-debug-tools';
5959
import {
6060
patch as patchConsole,
6161
registerRenderer as registerRendererWithConsole,
62+
patchForStrictMode as patchConsoleForStrictMode,
63+
unpatchForStrictMode as unpatchConsoleForStrictMode,
6264
} from './console';
6365
import {
6466
CONCURRENT_MODE_NUMBER,
@@ -4249,6 +4251,7 @@ export function attach(
42494251
handlePostCommitFiberRoot,
42504252
inspectElement,
42514253
logElementToConsole,
4254+
patchConsoleForStrictMode,
42524255
prepareViewAttributeSource,
42534256
prepareViewElementSource,
42544257
overrideError,
@@ -4261,6 +4264,7 @@ export function attach(
42614264
startProfiling,
42624265
stopProfiling,
42634266
storeAsGlobal,
4267+
unpatchConsoleForStrictMode,
42644268
updateComponentFilters,
42654269
};
42664270
}

packages/react-devtools-shared/src/backend/types.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,6 @@ export type ReactRenderer = {
136136
// Only injected by React v16.9+ in DEV mode.
137137
// Enables DevTools to append owners-only component stack to error messages.
138138
getCurrentFiber?: () => Fiber | null,
139-
140-
getIsStrictMode?: () => boolean,
141139
// 17.0.2+
142140
reconcilerVersion?: string,
143141
// Uniquely identifies React DOM v15.
@@ -352,6 +350,7 @@ export type RendererInterface = {
352350
path: Array<string | number>,
353351
value: any,
354352
) => void,
353+
patchConsoleForStrictMode: () => void,
355354
prepareViewAttributeSource: (
356355
id: number,
357356
path: Array<string | number>,
@@ -374,6 +373,7 @@ export type RendererInterface = {
374373
path: Array<string | number>,
375374
count: number,
376375
) => void,
376+
unpatchConsoleForStrictMode: () => void,
377377
updateComponentFilters: (componentFilters: Array<ComponentFilter>) => void,
378378
...
379379
};
@@ -408,5 +408,8 @@ export type DevToolsHook = {
408408
// Added in v16.9 to support Fast Refresh
409409
didError?: boolean,
410410
) => void,
411+
412+
// Testing
413+
dangerous_setTargetConsoleForTesting?: (fakeConsole: Object) => void,
411414
...
412415
};

0 commit comments

Comments
 (0)