|
1 | | -import React, { type JSX } from 'react' |
2 | | -import { CatchErrorBoundary } from './catch-error-boundary' |
| 1 | +'use client' |
| 2 | + |
| 3 | +import React, { startTransition, type JSX } from 'react' |
| 4 | +import { HandleISRError } from './handle-isr-error' |
| 5 | +import { handleHardNavError } from './nav-failure-handler' |
| 6 | +import { useUntrackedPathname } from './navigation-untracked' |
| 7 | +import { unstable_rethrow } from './unstable-rethrow' |
| 8 | +import { isBot } from '../../shared/lib/router/utils/is-bot' |
| 9 | +import { |
| 10 | + AppRouterContext, |
| 11 | + type AppRouterInstance, |
| 12 | +} from '../../shared/lib/app-router-context.shared-runtime' |
| 13 | +import { RouterContext } from '../../shared/lib/router-context.shared-runtime' |
3 | 14 | import type { ErrorInfo } from './error-boundary' |
4 | 15 |
|
| 16 | +const isBotUserAgent = |
| 17 | + typeof window !== 'undefined' && isBot(window.navigator.userAgent) |
| 18 | + |
5 | 19 | export type FallbackComponent<P> = ( |
6 | 20 | props: P & { children?: React.ReactNode }, |
7 | 21 | errorInfo: ErrorInfo |
8 | 22 | ) => React.ReactNode |
9 | 23 |
|
| 24 | +type CatchErrorWrapperProps<P> = P & { children?: React.ReactNode } |
| 25 | +type NextErrorBoundaryProps<P> = CatchErrorWrapperProps<P> & { |
| 26 | + pathname: string | null |
| 27 | + isPagesRouter: boolean |
| 28 | +} |
| 29 | + |
| 30 | +type NextErrorBoundaryState = { |
| 31 | + error: Error | null |
| 32 | + previousPathname: string | null |
| 33 | + componentStack: React.ErrorInfo['componentStack'] |
| 34 | + ownerStack: ReturnType<typeof React.captureOwnerStack> |
| 35 | +} |
| 36 | + |
| 37 | +type FallbackWrapperProps<P> = { |
| 38 | + props: P |
| 39 | + errorInfo: ErrorInfo |
| 40 | +} |
| 41 | + |
| 42 | +function createFallbackWrapper<P extends Record<string, any>>( |
| 43 | + fallback: FallbackComponent<P> |
| 44 | +): React.ComponentType<FallbackWrapperProps<P>> { |
| 45 | + const fallbackName = fallback.name || 'Unknown' |
| 46 | + const fallbackWrapper = { |
| 47 | + [fallbackName]: ({ props, errorInfo }: FallbackWrapperProps<P>) => |
| 48 | + fallback(props, errorInfo), |
| 49 | + }[fallbackName] as React.ComponentType<FallbackWrapperProps<P>> |
| 50 | + |
| 51 | + if (process.env.NODE_ENV !== 'production') { |
| 52 | + fallbackWrapper.displayName = fallbackName |
| 53 | + } |
| 54 | + |
| 55 | + return fallbackWrapper |
| 56 | +} |
| 57 | + |
10 | 58 | export function catchError<P extends Record<string, any>>( |
11 | 59 | fallback: FallbackComponent<P> |
12 | 60 | ): React.ComponentType<P & { children?: React.ReactNode }> { |
13 | | - function CatchErrorWrapper( |
14 | | - props: P & { children?: React.ReactNode } |
15 | | - ): JSX.Element { |
16 | | - const { children, ...componentProps } = props |
| 61 | + const FallbackWrapper = createFallbackWrapper(fallback) |
| 62 | + |
| 63 | + class NextErrorBoundary extends React.Component< |
| 64 | + NextErrorBoundaryProps<P>, |
| 65 | + NextErrorBoundaryState |
| 66 | + > { |
| 67 | + static contextType = AppRouterContext |
| 68 | + declare context: AppRouterInstance | null |
| 69 | + |
| 70 | + constructor(props: NextErrorBoundaryProps<P>) { |
| 71 | + super(props) |
| 72 | + this.state = { |
| 73 | + error: null, |
| 74 | + previousPathname: this.props.pathname, |
| 75 | + componentStack: undefined, |
| 76 | + ownerStack: null, |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + static getDerivedStateFromError(error: Error): { |
| 81 | + error: Error |
| 82 | + ownerStack: ReturnType<typeof React.captureOwnerStack> |
| 83 | + } { |
| 84 | + unstable_rethrow(error) |
| 85 | + |
| 86 | + let ownerStack: string | null = null |
| 87 | + if ('captureOwnerStack' in React) { |
| 88 | + ownerStack = React.captureOwnerStack() |
| 89 | + } |
| 90 | + |
| 91 | + return { error, ownerStack } |
| 92 | + } |
| 93 | + |
| 94 | + componentDidCatch(_error: Error, errorInfo: React.ErrorInfo): void { |
| 95 | + this.setState({ |
| 96 | + componentStack: errorInfo.componentStack, |
| 97 | + }) |
| 98 | + } |
| 99 | + |
| 100 | + static getDerivedStateFromProps( |
| 101 | + props: NextErrorBoundaryProps<P>, |
| 102 | + state: NextErrorBoundaryState |
| 103 | + ): NextErrorBoundaryState | null { |
| 104 | + const { error } = state |
| 105 | + |
| 106 | + // If we encounter an error while a navigation is pending, don't render |
| 107 | + // the fallback and let the hard navigation attempt to recover instead. |
| 108 | + if (process.env.__NEXT_APP_NAV_FAIL_HANDLING) { |
| 109 | + if (error && handleHardNavError(error)) { |
| 110 | + return { |
| 111 | + error: null, |
| 112 | + previousPathname: props.pathname, |
| 113 | + componentStack: undefined, |
| 114 | + ownerStack: null, |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + // Reset error state when navigation changes pathname so boundaries don't |
| 120 | + // stay latched while navigating to a different route. |
| 121 | + if (props.pathname !== state.previousPathname && error) { |
| 122 | + return { |
| 123 | + error: null, |
| 124 | + previousPathname: props.pathname, |
| 125 | + componentStack: undefined, |
| 126 | + ownerStack: null, |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + return { |
| 131 | + error: state.error, |
| 132 | + previousPathname: props.pathname, |
| 133 | + componentStack: state.componentStack, |
| 134 | + ownerStack: state.ownerStack, |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + clearError = () => { |
| 139 | + this.setState({ |
| 140 | + error: null, |
| 141 | + componentStack: undefined, |
| 142 | + ownerStack: null, |
| 143 | + }) |
| 144 | + } |
| 145 | + |
| 146 | + reset = () => { |
| 147 | + this.clearError() |
| 148 | + } |
| 149 | + |
| 150 | + retry = () => { |
| 151 | + const router = this.context |
| 152 | + if (this.props.isPagesRouter || router === null) { |
| 153 | + throw new Error( |
| 154 | + '`retry()` can only be used in the App Router. Use `reset()` in the Pages Router.' |
| 155 | + ) |
| 156 | + } |
| 157 | + |
| 158 | + startTransition(() => { |
| 159 | + router.refresh() |
| 160 | + this.clearError() |
| 161 | + }) |
| 162 | + } |
| 163 | + |
| 164 | + render(): React.ReactNode { |
| 165 | + if (this.state.error && !isBotUserAgent) { |
| 166 | + const { |
| 167 | + children: _children, |
| 168 | + pathname: _pathname, |
| 169 | + ...componentProps |
| 170 | + } = this.props |
| 171 | + const errorInfo: ErrorInfo = { |
| 172 | + error: this.state.error, |
| 173 | + reset: this.reset, |
| 174 | + retry: this.retry, |
| 175 | + componentStack: this.state.componentStack, |
| 176 | + ownerStack: this.state.ownerStack, |
| 177 | + } |
| 178 | + |
| 179 | + return ( |
| 180 | + <> |
| 181 | + <HandleISRError error={this.state.error} /> |
| 182 | + <FallbackWrapper |
| 183 | + props={componentProps as unknown as P} |
| 184 | + errorInfo={errorInfo} |
| 185 | + /> |
| 186 | + </> |
| 187 | + ) |
| 188 | + } |
| 189 | + |
| 190 | + return this.props.children |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + if (process.env.NODE_ENV !== 'production') { |
| 195 | + ;(NextErrorBoundary as any).displayName = 'Next.ErrorBoundary' |
| 196 | + } |
17 | 197 |
|
| 198 | + function CatchErrorWrapper(props: CatchErrorWrapperProps<P>): JSX.Element { |
| 199 | + const pathname = useUntrackedPathname() |
| 200 | + const pagesRouter = React.useContext(RouterContext) |
18 | 201 | return ( |
19 | | - <CatchErrorBoundary |
20 | | - fallback={fallback} |
21 | | - componentProps={componentProps as P} |
22 | | - > |
23 | | - {children} |
24 | | - </CatchErrorBoundary> |
| 202 | + <NextErrorBoundary |
| 203 | + {...props} |
| 204 | + pathname={pathname} |
| 205 | + isPagesRouter={pagesRouter !== null} |
| 206 | + /> |
25 | 207 | ) |
26 | 208 | } |
27 | 209 |
|
|
0 commit comments