Skip to content

Commit

Permalink
[Draft] don't patch console during first render (#22308)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lunaruan committed Sep 21, 2021
1 parent cf07c3d commit 5b57bc6
Show file tree
Hide file tree
Showing 18 changed files with 676 additions and 454 deletions.
222 changes: 172 additions & 50 deletions packages/react-devtools-shared/src/__tests__/console-test.js

Large diffs are not rendered by default.

337 changes: 200 additions & 137 deletions packages/react-devtools-shared/src/backend/console.js

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions packages/react-devtools-shared/src/backend/legacy/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1073,6 +1073,10 @@ export function attach(
// Not implemented
}

function patchConsoleForStrictMode() {}

function unpatchConsoleForStrictMode() {}

return {
clearErrorsAndWarnings,
clearErrorsForFiberID,
Expand Down Expand Up @@ -1101,6 +1105,7 @@ export function attach(
overrideSuspense,
overrideValueAtPath,
renamePath,
patchConsoleForStrictMode,
prepareViewAttributeSource,
prepareViewElementSource,
renderer,
Expand All @@ -1109,6 +1114,7 @@ export function attach(
startProfiling,
stopProfiling,
storeAsGlobal,
unpatchConsoleForStrictMode,
updateComponentFilters,
};
}
4 changes: 4 additions & 0 deletions packages/react-devtools-shared/src/backend/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ import {inspectHooksOfFiber} from 'react-debug-tools';
import {
patch as patchConsole,
registerRenderer as registerRendererWithConsole,
patchForStrictMode as patchConsoleForStrictMode,
unpatchForStrictMode as unpatchConsoleForStrictMode,
} from './console';
import {
CONCURRENT_MODE_NUMBER,
Expand Down Expand Up @@ -4249,6 +4251,7 @@ export function attach(
handlePostCommitFiberRoot,
inspectElement,
logElementToConsole,
patchConsoleForStrictMode,
prepareViewAttributeSource,
prepareViewElementSource,
overrideError,
Expand All @@ -4261,6 +4264,7 @@ export function attach(
startProfiling,
stopProfiling,
storeAsGlobal,
unpatchConsoleForStrictMode,
updateComponentFilters,
};
}
7 changes: 5 additions & 2 deletions packages/react-devtools-shared/src/backend/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,6 @@ export type ReactRenderer = {
// Only injected by React v16.9+ in DEV mode.
// Enables DevTools to append owners-only component stack to error messages.
getCurrentFiber?: () => Fiber | null,

getIsStrictMode?: () => boolean,
// 17.0.2+
reconcilerVersion?: string,
// Uniquely identifies React DOM v15.
Expand Down Expand Up @@ -352,6 +350,7 @@ export type RendererInterface = {
path: Array<string | number>,
value: any,
) => void,
patchConsoleForStrictMode: () => void,
prepareViewAttributeSource: (
id: number,
path: Array<string | number>,
Expand All @@ -374,6 +373,7 @@ export type RendererInterface = {
path: Array<string | number>,
count: number,
) => void,
unpatchConsoleForStrictMode: () => void,
updateComponentFilters: (componentFilters: Array<ComponentFilter>) => void,
...
};
Expand Down Expand Up @@ -408,5 +408,8 @@ export type DevToolsHook = {
// Added in v16.9 to support Fast Refresh
didError?: boolean,
) => void,

// Testing
dangerous_setTargetConsoleForTesting?: (fakeConsole: Object) => void,
...
};
Loading

0 comments on commit 5b57bc6

Please sign in to comment.