Skip to content

Commit f6e1243

Browse files
committed
Use AppRouterContext instead of publicAppRouterInstance
error-boundary.tsx imported publicAppRouterInstance from app-router-instance.ts, which transitively pulls in the entire App Router module graph (router-reducer → fetch-server-response → react-server-dom-webpack/client). This broke any non-App-Router context (e.g. Pages Router) that imports a module depending on error-boundary.tsx, since react-server-dom-webpack/client is unavailable there. Replace with AppRouterContext from the lightweight shared-runtime module, which only contains type imports and React context creation — no heavy runtime dependencies. The error boundary already renders inside the AppRouterContext.Provider scope, so this.context always has the router instance for segment-level boundaries.
1 parent bf76978 commit f6e1243

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

packages/next/src/client/components/error-boundary.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import { isNextRouterError } from './is-next-router-error'
66
import { handleHardNavError } from './nav-failure-handler'
77
import { HandleISRError } from './handle-isr-error'
88
import { isBot } from '../../shared/lib/router/utils/is-bot'
9-
import { publicAppRouterInstance } from './app-router-instance'
9+
import {
10+
AppRouterContext,
11+
type AppRouterInstance,
12+
} from '../../shared/lib/app-router-context.shared-runtime'
1013

1114
const isBotUserAgent =
1215
typeof window !== 'undefined' && isBot(window.navigator.userAgent)
@@ -43,6 +46,9 @@ export class ErrorBoundaryHandler extends React.Component<
4346
ErrorBoundaryHandlerProps,
4447
ErrorBoundaryHandlerState
4548
> {
49+
static contextType = AppRouterContext
50+
declare context: AppRouterInstance | null
51+
4652
constructor(props: ErrorBoundaryHandlerProps) {
4753
super(props)
4854
this.state = {
@@ -128,7 +134,7 @@ export class ErrorBoundaryHandler extends React.Component<
128134

129135
retry = () => {
130136
startTransition(() => {
131-
publicAppRouterInstance.refresh()
137+
this.context?.refresh()
132138
this.reset()
133139
})
134140
}

packages/next/src/next-devtools/userspace/app/app-dev-overlay-error-boundary.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ import { RuntimeErrorHandler } from '../../../client/dev/runtime-error-handler'
44
import { ErrorBoundary } from '../../../client/components/error-boundary'
55
import DefaultGlobalError from '../../../client/components/builtin/global-error'
66
import type { GlobalErrorState } from '../../../client/components/app-router-instance'
7-
import { publicAppRouterInstance } from '../../../client/components/app-router-instance'
87
import { SEGMENT_EXPLORER_SIMULATED_ERROR_MESSAGE } from './segment-explorer-node'
8+
import {
9+
AppRouterContext,
10+
type AppRouterInstance,
11+
} from '../../../shared/lib/app-router-context.shared-runtime'
912

1013
type AppDevOverlayErrorBoundaryProps = {
1114
children: React.ReactNode
@@ -60,6 +63,9 @@ export class AppDevOverlayErrorBoundary extends PureComponent<
6063
AppDevOverlayErrorBoundaryProps,
6164
AppDevOverlayErrorBoundaryState
6265
> {
66+
static contextType = AppRouterContext
67+
declare context: AppRouterInstance | null
68+
6369
state: AppDevOverlayErrorBoundaryState = {
6470
reactError: null,
6571
componentStack: undefined,
@@ -95,7 +101,7 @@ export class AppDevOverlayErrorBoundary extends PureComponent<
95101

96102
retry = () => {
97103
startTransition(() => {
98-
publicAppRouterInstance.refresh()
104+
this.context?.refresh()
99105
this.reset()
100106
})
101107
}

0 commit comments

Comments
 (0)