Skip to content

Commit

Permalink
Align onRecoverableError callback between pages and app dir (#44161)
Browse files Browse the repository at this point in the history
Should not re-throw the error on client for errors from
`onRecoverableError` callback in pages, this PR share it as a same
callback between pages and app dir

follow up for #42589
  • Loading branch information
huozhi committed Dec 19, 2022
1 parent 6827b58 commit 13a9fb9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 24 deletions.
19 changes: 1 addition & 18 deletions packages/next/client/app-index.tsx
Expand Up @@ -7,7 +7,7 @@ import { createFromReadableStream } from 'next/dist/compiled/react-server-dom-we

import { HeadManagerContext } from '../shared/lib/head-manager-context'
import { GlobalLayoutRouterContext } from '../shared/lib/app-router-context'
import { NEXT_DYNAMIC_NO_SSR_CODE } from '../shared/lib/no-ssr-error'
import onRecoverableError from './on-recoverable-error'

/// <reference types="react-dom/experimental" />

Expand Down Expand Up @@ -130,23 +130,6 @@ if (document.readyState === 'loading') {
DOMContentLoaded()
}

function onRecoverableError(err: any) {
// Using default react onRecoverableError
// x-ref: https://github.com/facebook/react/blob/d4bc16a7d69eb2ea38a88c8ac0b461d5f72cdcab/packages/react-dom/src/client/ReactDOMRoot.js#L83
const defaultOnRecoverableError =
typeof reportError === 'function'
? // In modern browsers, reportError will dispatch an error event,
// emulating an uncaught JavaScript error.
reportError
: (error: any) => {
window.console.error(error)
}

// Skip certain custom errors which are not expected to be reported on client
if (err.digest === NEXT_DYNAMIC_NO_SSR_CODE) return
defaultOnRecoverableError(err)
}

const nextServerDataLoadingGlobal = ((self as any).__next_f =
(self as any).__next_f || [])
nextServerDataLoadingGlobal.forEach(nextServerDataCallback)
Expand Down
8 changes: 2 additions & 6 deletions packages/next/client/index.tsx
Expand Up @@ -43,7 +43,7 @@ import {
PathnameContextProviderAdapter,
} from '../shared/lib/router/adapters'
import { SearchParamsContext } from '../shared/lib/hooks-client-context'
import { NEXT_DYNAMIC_NO_SSR_CODE } from '../shared/lib/no-ssr-error'
import onRecoverableError from './on-recoverable-error'

/// <reference types="react-dom/experimental" />

Expand Down Expand Up @@ -507,11 +507,7 @@ function renderReactElement(
if (!reactRoot) {
// Unlike with createRoot, you don't need a separate root.render() call here
reactRoot = ReactDOM.hydrateRoot(domEl, reactEl, {
onRecoverableError(err: any) {
// Skip certain custom errors which are not expected to throw on client
if (err.message === NEXT_DYNAMIC_NO_SSR_CODE) return
throw err
},
onRecoverableError,
})
// TODO: Remove shouldHydrate variable when React 18 is stable as it can depend on `reactRoot` existing
shouldHydrate = false
Expand Down
18 changes: 18 additions & 0 deletions packages/next/client/on-recoverable-error.ts
@@ -0,0 +1,18 @@
import { NEXT_DYNAMIC_NO_SSR_CODE } from '../shared/lib/no-ssr-error'

export default function onRecoverableError(err: any) {
// Using default react onRecoverableError
// x-ref: https://github.com/facebook/react/blob/d4bc16a7d69eb2ea38a88c8ac0b461d5f72cdcab/packages/react-dom/src/client/ReactDOMRoot.js#L83
const defaultOnRecoverableError =
typeof reportError === 'function'
? // In modern browsers, reportError will dispatch an error event,
// emulating an uncaught JavaScript error.
reportError
: (error: any) => {
window.console.error(error)
}

// Skip certain custom errors which are not expected to be reported on client
if (err.digest === NEXT_DYNAMIC_NO_SSR_CODE) return
defaultOnRecoverableError(err)
}

0 comments on commit 13a9fb9

Please sign in to comment.