File size: 17,239 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 |
import { useReducer } from 'react'
import type { VersionInfo } from '../../server/dev/parse-version-info'
import type { SupportedErrorEvent } from './container/runtime-error/render-error'
import { parseComponentStack } from './utils/parse-component-stack'
import type { DebugInfo } from '../shared/types'
import type { DevIndicatorServerState } from '../../server/dev/dev-indicator-server-state'
import { parseStack } from '../../server/lib/parse-stack'
import { isConsoleError } from '../shared/console-error'
export type DevToolsConfig = {
theme?: 'light' | 'dark' | 'system'
disableDevIndicator?: boolean
devToolsPosition?: 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right'
devToolsPanelPosition?: Record<
string,
'top-left' | 'top-right' | 'bottom-left' | 'bottom-right'
>
devToolsPanelSize?: Record<string, { width: number; height: number }>
scale?: number
hideShortcut?: string | null
}
export type Corners = 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right'
export type DevToolsIndicatorPosition = Corners
const BASE_SIZE = 16
export const NEXT_DEV_TOOLS_SCALE = {
Small: BASE_SIZE / 14,
Medium: BASE_SIZE / 16,
Large: BASE_SIZE / 18,
}
export type DevToolsScale =
(typeof NEXT_DEV_TOOLS_SCALE)[keyof typeof NEXT_DEV_TOOLS_SCALE]
type FastRefreshState =
/** No refresh in progress. */
| { type: 'idle' }
/** The refresh process has been triggered, but the new code has not been executed yet. */
| { type: 'pending'; errors: SupportedErrorEvent[] }
export interface OverlayState {
nextId: number
buildError: string | null
errors: SupportedErrorEvent[]
refreshState: FastRefreshState
versionInfo: VersionInfo
notFound: boolean
buildingIndicator: boolean
renderingIndicator: boolean
staticIndicator: boolean
showIndicator: boolean
disableDevIndicator: boolean
/** Whether to show the restart server button in the panel UI. Currently
* only used when Turbopack + Persistent Cache is enabled.
*/
showRestartServerButton: boolean
debugInfo: DebugInfo
routerType: 'pages' | 'app'
/** This flag is used to handle the Error Overlay state in the "old" overlay.
* In the DevTools panel, this value will used for the "Error Overlay Mode"
* which is viewing the "Issues Tab" as a fullscreen.
*/
isErrorOverlayOpen: boolean
devToolsPosition: Corners
devToolsPanelPosition: Record<DevtoolsPanelName, Corners>
devToolsPanelSize: Record<
DevtoolsPanelName,
{ width: number; height: number }
>
scale: number
page: string
theme: 'light' | 'dark' | 'system'
hideShortcut: string | null
}
type DevtoolsPanelName = string
export type OverlayDispatch = React.Dispatch<DispatcherEvent>
export const ACTION_STATIC_INDICATOR = 'static-indicator'
export const ACTION_BUILD_OK = 'build-ok'
export const ACTION_BUILD_ERROR = 'build-error'
export const ACTION_BEFORE_REFRESH = 'before-fast-refresh'
export const ACTION_REFRESH = 'fast-refresh'
export const ACTION_VERSION_INFO = 'version-info'
export const ACTION_UNHANDLED_ERROR = 'unhandled-error'
export const ACTION_UNHANDLED_REJECTION = 'unhandled-rejection'
export const ACTION_DEBUG_INFO = 'debug-info'
export const ACTION_DEV_INDICATOR = 'dev-indicator'
export const ACTION_DEV_INDICATOR_SET = 'dev-indicator-disable'
export const ACTION_ERROR_OVERLAY_OPEN = 'error-overlay-open'
export const ACTION_ERROR_OVERLAY_CLOSE = 'error-overlay-close'
export const ACTION_ERROR_OVERLAY_TOGGLE = 'error-overlay-toggle'
export const ACTION_BUILDING_INDICATOR_SHOW = 'building-indicator-show'
export const ACTION_BUILDING_INDICATOR_HIDE = 'building-indicator-hide'
export const ACTION_RENDERING_INDICATOR_SHOW = 'rendering-indicator-show'
export const ACTION_RENDERING_INDICATOR_HIDE = 'rendering-indicator-hide'
export const ACTION_DEVTOOLS_PANEL_OPEN = 'devtools-panel-open'
export const ACTION_DEVTOOLS_PANEL_CLOSE = 'devtools-panel-close'
export const ACTION_DEVTOOLS_PANEL_TOGGLE = 'devtools-panel-toggle'
export const ACTION_DEVTOOLS_POSITION = 'devtools-position'
export const ACTION_DEVTOOLS_PANEL_POSITION = 'devtools-panel-position'
export const ACTION_DEVTOOLS_PANEL_SIZE = 'devtools-panel-size'
export const ACTION_DEVTOOLS_SCALE = 'devtools-scale'
export const ACTION_RESTART_SERVER_BUTTON = 'restart-server-button'
export const ACTION_DEVTOOLS_CONFIG = 'devtools-config'
export const STORAGE_KEY_THEME = '__nextjs-dev-tools-theme'
export const STORAGE_KEY_POSITION = '__nextjs-dev-tools-position'
export const STORAGE_KEY_PANEL_POSITION_PREFIX =
'__nextjs-dev-tools-panel-position'
export const STORE_KEY_PANEL_SIZE_PREFIX = '__nextjs-dev-tools-panel-size'
export const STORE_KEY_SHARED_PANEL_SIZE =
'__nextjs-dev-tools-shared-panel-size'
export const STORE_KEY_SHARED_PANEL_LOCATION =
'__nextjs-dev-tools-shared-panel-location'
export const STORAGE_KEY_SCALE = '__nextjs-dev-tools-scale'
export const ACTION_DEVTOOL_UPDATE_ROUTE_STATE =
'segment-explorer-update-route-state'
interface StaticIndicatorAction {
type: typeof ACTION_STATIC_INDICATOR
staticIndicator: boolean
}
interface BuildOkAction {
type: typeof ACTION_BUILD_OK
}
interface BuildErrorAction {
type: typeof ACTION_BUILD_ERROR
message: string
}
interface BeforeFastRefreshAction {
type: typeof ACTION_BEFORE_REFRESH
}
interface FastRefreshAction {
type: typeof ACTION_REFRESH
}
export interface UnhandledErrorAction {
type: typeof ACTION_UNHANDLED_ERROR
reason: Error
}
export interface UnhandledRejectionAction {
type: typeof ACTION_UNHANDLED_REJECTION
reason: Error
}
export interface DebugInfoAction {
type: typeof ACTION_DEBUG_INFO
debugInfo: any
}
interface VersionInfoAction {
type: typeof ACTION_VERSION_INFO
versionInfo: VersionInfo
}
interface DevIndicatorAction {
type: typeof ACTION_DEV_INDICATOR
devIndicator: DevIndicatorServerState
}
interface DevIndicatorSetAction {
type: typeof ACTION_DEV_INDICATOR_SET
disabled: boolean
}
export interface ErrorOverlayOpenAction {
type: typeof ACTION_ERROR_OVERLAY_OPEN
}
export interface ErrorOverlayCloseAction {
type: typeof ACTION_ERROR_OVERLAY_CLOSE
}
export interface ErrorOverlayToggleAction {
type: typeof ACTION_ERROR_OVERLAY_TOGGLE
}
export interface BuildingIndicatorShowAction {
type: typeof ACTION_BUILDING_INDICATOR_SHOW
}
export interface BuildingIndicatorHideAction {
type: typeof ACTION_BUILDING_INDICATOR_HIDE
}
export interface RenderingIndicatorShowAction {
type: typeof ACTION_RENDERING_INDICATOR_SHOW
}
export interface RenderingIndicatorHideAction {
type: typeof ACTION_RENDERING_INDICATOR_HIDE
}
export interface DevToolsIndicatorPositionAction {
type: typeof ACTION_DEVTOOLS_POSITION
devToolsPosition: Corners
}
export interface DevToolsPanelPositionAction {
type: typeof ACTION_DEVTOOLS_PANEL_POSITION
key: string
devToolsPanelPosition: Corners
}
export interface DevToolsScaleAction {
type: typeof ACTION_DEVTOOLS_SCALE
scale: number
}
export interface DevToolUpdateRouteStateAction {
type: typeof ACTION_DEVTOOL_UPDATE_ROUTE_STATE
page: string
}
export interface RestartServerButtonAction {
type: typeof ACTION_RESTART_SERVER_BUTTON
showRestartServerButton: boolean
}
export interface DevToolsConfigAction {
type: typeof ACTION_DEVTOOLS_CONFIG
devToolsConfig: DevToolsConfig
}
export type DispatcherEvent =
| BuildOkAction
| BuildErrorAction
| BeforeFastRefreshAction
| FastRefreshAction
| UnhandledErrorAction
| UnhandledRejectionAction
| VersionInfoAction
| StaticIndicatorAction
| DebugInfoAction
| DevIndicatorAction
| ErrorOverlayOpenAction
| ErrorOverlayCloseAction
| ErrorOverlayToggleAction
| BuildingIndicatorShowAction
| BuildingIndicatorHideAction
| RenderingIndicatorShowAction
| RenderingIndicatorHideAction
| DevToolsIndicatorPositionAction
| DevToolsPanelPositionAction
| DevToolsScaleAction
| DevToolUpdateRouteStateAction
| RestartServerButtonAction
| DevIndicatorSetAction
| DevToolsConfigAction
const REACT_ERROR_STACK_BOTTOM_FRAME_REGEX =
// 1st group: new frame + v8
// 2nd group: new frame + SpiderMonkey, JavaScriptCore
// 3rd group: old frame + v8
// 4th group: old frame + SpiderMonkey, JavaScriptCore
/\s+(at Object\.react_stack_bottom_frame.*)|(react_stack_bottom_frame@.*)|(at react-stack-bottom-frame.*)|(react-stack-bottom-frame@.*)/
// React calls user code starting from a special stack frame.
// The basic stack will be different if the same error location is hit again
// due to StrictMode.
// This gets only the stack after React which is unaffected by StrictMode.
function getStackIgnoringStrictMode(stack: string | undefined) {
return stack?.split(REACT_ERROR_STACK_BOTTOM_FRAME_REGEX)[0]
}
const shouldDisableDevIndicator =
process.env.__NEXT_DEV_INDICATOR?.toString() === 'false'
export const INITIAL_OVERLAY_STATE: Omit<
OverlayState,
'isErrorOverlayOpen' | 'routerType'
> = {
nextId: 1,
buildError: null,
errors: [],
notFound: false,
renderingIndicator: false,
staticIndicator: false,
/*
This is set to `true` when we can reliably know
whether the indicator is in disabled state or not.
Otherwise the surface would flicker because the disabled flag loads from the config.
*/
showIndicator: false,
disableDevIndicator: false,
buildingIndicator: false,
refreshState: { type: 'idle' },
versionInfo: { installed: '0.0.0', staleness: 'unknown' },
debugInfo: { devtoolsFrontendUrl: undefined },
showRestartServerButton: false,
devToolsPosition: 'bottom-left',
devToolsPanelPosition: {
[STORE_KEY_SHARED_PANEL_LOCATION]: 'bottom-left',
},
devToolsPanelSize: {},
scale: NEXT_DEV_TOOLS_SCALE.Medium,
page: '',
theme: 'system',
hideShortcut: null,
}
function getInitialState(
routerType: 'pages' | 'app'
): OverlayState & { routerType: 'pages' | 'app' } {
return {
...INITIAL_OVERLAY_STATE,
// Pages Router only listenes to thrown errors which
// always open the overlay.
// TODO: Should be the same default as App Router once we surface console.error in Pages Router.
isErrorOverlayOpen: routerType === 'pages',
routerType,
}
}
export function useErrorOverlayReducer(
routerType: 'pages' | 'app',
getComponentStack: (error: Error) => string | undefined,
getOwnerStack: (error: Error) => string | null | undefined,
isRecoverableError: (error: Error) => boolean
) {
function pushErrorFilterDuplicates(
events: SupportedErrorEvent[],
id: number,
error: Error
): SupportedErrorEvent[] {
const componentStack = getComponentStack(error)
const componentStackFrames =
componentStack === undefined
? undefined
: parseComponentStack(componentStack)
const ownerStack = getOwnerStack(error)
const frames = parseStack((error.stack || '') + (ownerStack || ''))
const pendingEvent: SupportedErrorEvent = {
id,
error,
frames,
componentStackFrames,
type: isRecoverableError(error)
? 'recoverable'
: isConsoleError(error)
? 'console'
: 'runtime',
}
const pendingEvents = events.filter((event) => {
// Filter out duplicate errors
return (
(event.error.stack !== pendingEvent.error.stack &&
// TODO: Let ReactDevTools control deduping instead?
getStackIgnoringStrictMode(event.error.stack) !==
getStackIgnoringStrictMode(pendingEvent.error.stack)) ||
getOwnerStack(event.error) !== getOwnerStack(pendingEvent.error)
)
})
// If there's nothing filtered out, the event is a brand new error
if (pendingEvents.length === events.length) {
pendingEvents.push(pendingEvent)
return pendingEvents
}
// Otherwise remain the same events
return events
}
return useReducer(
(state: OverlayState, action: DispatcherEvent): OverlayState => {
switch (action.type) {
case ACTION_DEBUG_INFO: {
return { ...state, debugInfo: action.debugInfo }
}
case ACTION_STATIC_INDICATOR: {
return { ...state, staticIndicator: action.staticIndicator }
}
case ACTION_BUILD_OK: {
return { ...state, buildError: null }
}
case ACTION_BUILD_ERROR: {
return { ...state, buildError: action.message }
}
case ACTION_BEFORE_REFRESH: {
return { ...state, refreshState: { type: 'pending', errors: [] } }
}
case ACTION_REFRESH: {
return {
...state,
buildError: null,
errors:
// Errors can come in during updates. In this case, UNHANDLED_ERROR
// and UNHANDLED_REJECTION events might be dispatched between the
// BEFORE_REFRESH and the REFRESH event. We want to keep those errors
// around until the next refresh. Otherwise we run into a race
// condition where those errors would be cleared on refresh completion
// before they can be displayed.
state.refreshState.type === 'pending'
? state.refreshState.errors
: [],
refreshState: { type: 'idle' },
}
}
case ACTION_UNHANDLED_ERROR:
case ACTION_UNHANDLED_REJECTION: {
switch (state.refreshState.type) {
case 'idle': {
return {
...state,
nextId: state.nextId + 1,
errors: pushErrorFilterDuplicates(
state.errors,
state.nextId,
action.reason
),
}
}
case 'pending': {
return {
...state,
nextId: state.nextId + 1,
refreshState: {
...state.refreshState,
errors: pushErrorFilterDuplicates(
state.errors,
state.nextId,
action.reason
),
},
}
}
default:
return state
}
}
case ACTION_VERSION_INFO: {
return { ...state, versionInfo: action.versionInfo }
}
case ACTION_DEV_INDICATOR_SET: {
return { ...state, disableDevIndicator: action.disabled }
}
case ACTION_DEV_INDICATOR: {
return {
...state,
showIndicator: true,
disableDevIndicator:
shouldDisableDevIndicator || !!action.devIndicator.disabledUntil,
}
}
case ACTION_ERROR_OVERLAY_OPEN: {
return { ...state, isErrorOverlayOpen: true }
}
case ACTION_ERROR_OVERLAY_CLOSE: {
return { ...state, isErrorOverlayOpen: false }
}
case ACTION_ERROR_OVERLAY_TOGGLE: {
return { ...state, isErrorOverlayOpen: !state.isErrorOverlayOpen }
}
case ACTION_BUILDING_INDICATOR_SHOW: {
return { ...state, buildingIndicator: true }
}
case ACTION_BUILDING_INDICATOR_HIDE: {
return { ...state, buildingIndicator: false }
}
case ACTION_RENDERING_INDICATOR_SHOW: {
return { ...state, renderingIndicator: true }
}
case ACTION_RENDERING_INDICATOR_HIDE: {
return { ...state, renderingIndicator: false }
}
case ACTION_DEVTOOLS_POSITION: {
return { ...state, devToolsPosition: action.devToolsPosition }
}
case ACTION_DEVTOOLS_PANEL_POSITION: {
return {
...state,
devToolsPanelPosition: {
...state.devToolsPanelPosition,
[action.key]: action.devToolsPanelPosition,
},
}
}
case ACTION_DEVTOOLS_SCALE: {
return { ...state, scale: action.scale }
}
case ACTION_DEVTOOL_UPDATE_ROUTE_STATE: {
return { ...state, page: action.page }
}
case ACTION_RESTART_SERVER_BUTTON: {
return {
...state,
showRestartServerButton: action.showRestartServerButton,
}
}
case ACTION_DEVTOOLS_CONFIG: {
const {
theme,
disableDevIndicator,
devToolsPosition,
devToolsPanelPosition,
devToolsPanelSize,
scale,
hideShortcut,
} = action.devToolsConfig
return {
...state,
theme: theme ?? state.theme,
disableDevIndicator:
disableDevIndicator ?? state.disableDevIndicator,
devToolsPosition: devToolsPosition ?? state.devToolsPosition,
devToolsPanelPosition:
devToolsPanelPosition ?? state.devToolsPanelPosition,
scale: scale ?? state.scale,
devToolsPanelSize: devToolsPanelSize ?? state.devToolsPanelSize,
hideShortcut:
// hideShortcut can be null.
hideShortcut !== undefined ? hideShortcut : state.hideShortcut,
}
}
default: {
return state
}
}
},
getInitialState(routerType)
)
}
|