-
Notifications
You must be signed in to change notification settings - Fork 27.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve hydration diff view (#75799)
- Loading branch information
Showing
16 changed files
with
949 additions
and
1,686 deletions.
There are no files selected for viewing
71 changes: 41 additions & 30 deletions
71
packages/next/src/client/components/errors/attach-hydration-error-state.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,57 @@ | ||
import { | ||
isHydrationError, | ||
getDefaultHydrationErrorMessage, | ||
testReactHydrationWarning, | ||
} from '../is-hydration-error' | ||
import { | ||
hydrationErrorState, | ||
getReactHydrationDiffSegments, | ||
} from './hydration-error-info' | ||
|
||
export function attachHydrationErrorState(error: Error) { | ||
if ( | ||
isHydrationError(error) && | ||
!error.message.includes( | ||
'https://nextjs.org/docs/messages/react-hydration-error' | ||
) | ||
) { | ||
const reactHydrationDiffSegments = getReactHydrationDiffSegments( | ||
error.message | ||
) | ||
let parsedHydrationErrorState: typeof hydrationErrorState = {} | ||
if (reactHydrationDiffSegments) { | ||
const reactHydrationDiffSegments = getReactHydrationDiffSegments( | ||
error.message | ||
) | ||
let parsedHydrationErrorState: typeof hydrationErrorState = {} | ||
const isHydrationWarning = testReactHydrationWarning(error.message) | ||
// If the reactHydrationDiffSegments exists | ||
// and the diff (reactHydrationDiffSegments[1]) exists | ||
// e.g. the hydration diff log error. | ||
if (reactHydrationDiffSegments && reactHydrationDiffSegments[1]) { | ||
parsedHydrationErrorState = { | ||
...(error as any).details, | ||
...hydrationErrorState, | ||
warning: hydrationErrorState.warning || [ | ||
getDefaultHydrationErrorMessage(), | ||
], | ||
notes: isHydrationWarning ? '' : reactHydrationDiffSegments[0], | ||
reactOutputComponentDiff: reactHydrationDiffSegments[1], | ||
} | ||
// Cache the `reactOutputComponentDiff` into hydrationErrorState. | ||
// This is only required for now when we still squashed the hydration diff log into hydration error. | ||
// Once the all error is logged to dev overlay in order, this will go away. | ||
hydrationErrorState.reactOutputComponentDiff = | ||
parsedHydrationErrorState.reactOutputComponentDiff | ||
} else { | ||
// Normal runtime error, where it doesn't contain the hydration diff. | ||
|
||
// If there's any extra information in the error message to display, | ||
// append it to the error message details property | ||
if (hydrationErrorState.warning) { | ||
// The patched console.error found hydration errors logged by React | ||
// Append the logged warning to the error message | ||
parsedHydrationErrorState = { | ||
...(error as any).details, | ||
// It contains the warning, component stack, server and client tag names | ||
...hydrationErrorState, | ||
warning: hydrationErrorState.warning || [ | ||
getDefaultHydrationErrorMessage(), | ||
], | ||
notes: reactHydrationDiffSegments[0], | ||
reactOutputComponentDiff: reactHydrationDiffSegments[1], | ||
} | ||
} else { | ||
// If there's any extra information in the error message to display, | ||
// append it to the error message details property | ||
if (hydrationErrorState.warning) { | ||
// The patched console.error found hydration errors logged by React | ||
// Append the logged warning to the error message | ||
parsedHydrationErrorState = { | ||
...(error as any).details, | ||
// It contains the warning, component stack, server and client tag names | ||
...hydrationErrorState, | ||
} | ||
} | ||
} | ||
;(error as any).details = parsedHydrationErrorState | ||
// Consume the cached hydration diff. | ||
// This is only required for now when we still squashed the hydration diff log into hydration error. | ||
// Once the all error is logged to dev overlay in order, this will go away. | ||
if (hydrationErrorState.reactOutputComponentDiff) { | ||
parsedHydrationErrorState.reactOutputComponentDiff = | ||
hydrationErrorState.reactOutputComponentDiff | ||
} | ||
} | ||
;(error as any).details = parsedHydrationErrorState | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.