Skip to content

Commit

Permalink
Fully enable custom error callbacks for app router (#71794)
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi authored Oct 24, 2024
1 parent af6f6aa commit ddc8807
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions packages/next/src/client/app-index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,12 @@ function Root({ children }: React.PropsWithChildren<{}>) {
return children
}

const reactRootOptions = {
onRecoverableError,
onCaughtError,
onUncaughtError,
} satisfies ReactDOMClient.RootOptions

export function hydrate() {
const reactEl = (
<StrictModeIfEnabled>
Expand All @@ -231,19 +237,6 @@ export function hydrate() {
const rootLayoutMissingTags = window.__next_root_layout_missing_tags
const hasMissingTags = !!rootLayoutMissingTags?.length

const errorCallbacks =
typeof (React as any).captureOwnerStack === 'function' &&
process.env.NODE_ENV !== 'production'
? {
onCaughtError,
onUncaughtError,
}
: undefined

const options = {
onRecoverableError,
...errorCallbacks,
} satisfies ReactDOMClient.RootOptions
const isError =
document.documentElement.id === '__next_error__' || hasMissingTags

Expand All @@ -252,14 +245,18 @@ export function hydrate() {
const createDevOverlayElement =
require('./components/react-dev-overlay/client-entry').createDevOverlayElement
const errorTree = createDevOverlayElement(reactEl)
ReactDOMClient.createRoot(appElement as any, options).render(errorTree)
ReactDOMClient.createRoot(appElement as any, reactRootOptions).render(
errorTree
)
} else {
ReactDOMClient.createRoot(appElement as any, options).render(reactEl)
ReactDOMClient.createRoot(appElement as any, reactRootOptions).render(
reactEl
)
}
} else {
React.startTransition(() =>
(ReactDOMClient as any).hydrateRoot(appElement, reactEl, {
...options,
...reactRootOptions,
formState: initialFormStateData,
})
)
Expand Down

0 comments on commit ddc8807

Please sign in to comment.