Unverified Commit 824f8537 authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

[devtools]: move RestartServerButton to panel footer (#81082)

This moves the restart button to the footer in the new panel UI. It also
properly gates to only be enabled with Turbopack + Persistent Cache.

Because it's no longer part of the error overlay, I refactored the logic
a bit to hoist the flag into overlay state and setup the beforeunload
listener from within the error renderer.

This heuristic still feels a bit unreliable and I'm not entirely
convinced how useful it is yet, but the goal with this PR is just to get
it to render in the right spot for now.

![CleanShot 2025-06-30 at 10 24
12@2x](https://github.com/user-attachments/assets/7c96b7ad-0b90-4a08-9b9d-5bea02348ab8)

Closes NEXT-4561
parent 62699817
......@@ -2,13 +2,16 @@ import type { OverlayState } from '../../shared'
import { DevToolsPanelVersionInfo } from './devtools-panel-version-info'
import { css } from '../../utils/css'
import { RestartServerButton } from '../errors/error-overlay-toolbar/restart-server-button'
export function DevToolsPanelFooter({
versionInfo,
isDraggable,
showRestartServerButton,
}: {
versionInfo: OverlayState['versionInfo']
isDraggable: boolean
showRestartServerButton: boolean
}) {
const bundlerName = (
process.env.__NEXT_BUNDLER || 'WEBPACK'
......@@ -32,6 +35,11 @@ export function DevToolsPanelFooter({
</span>
</div>
</div>
{showRestartServerButton && (
<div data-nextjs-devtools-panel-footer-tab-group>
<RestartServerButton showButton={true} />
</div>
)}
</div>
)
}
......
......@@ -231,6 +231,7 @@ export function DevToolsPanel({
<DevToolsPanelFooter
versionInfo={state.versionInfo}
isDraggable={!isFullscreen}
showRestartServerButton={state.showRestartServerButton}
/>
</Dialog>
</>
......
......@@ -61,6 +61,7 @@ const state: OverlayState = {
isErrorOverlayOpen: false,
// TODO: This will be handled on the next stack——with proper story.
isDevToolsPanelOpen: false,
showRestartServerButton: false,
devToolsPosition: 'bottom-left',
scale: 1,
page: '',
......
......@@ -2,7 +2,6 @@ import type { DebugInfo } from '../../../../shared/types'
import { NodejsInspectorButton } from './nodejs-inspector-button'
import { CopyStackTraceButton } from './copy-stack-trace-button'
import { DocsLinkButton } from './docs-link-button'
import { RestartServerButton } from './restart-server-button'
type ErrorOverlayToolbarProps = {
error: Error
......@@ -19,9 +18,6 @@ export function ErrorOverlayToolbar({
<span className="error-overlay-toolbar">
{/* TODO: Move the button inside and remove the feedback on the footer of the error overlay. */}
{feedbackButton}
{process.env.__NEXT_BUNDLER_HAS_PERSISTENT_CACHE && (
<RestartServerButton error={error} />
)}
<CopyStackTraceButton error={error} />
<DocsLinkButton errorMessage={error.message} />
<NodejsInspectorButton
......
import { useState, useEffect } from 'react'
import { useEffect } from 'react'
import { RefreshClockWise } from '../../../icons/refresh-clock-wise'
import {
ACTION_RESTART_SERVER_BUTTON,
type OverlayDispatch,
} from '../../../shared'
import type { SupportedErrorEvent } from '../../../container/runtime-error/render-error'
/**
* When the Turbopack persistent cache is enabled, and the user reloads on a
......@@ -8,24 +13,7 @@ import { RefreshClockWise } from '../../../icons/refresh-clock-wise'
* bundler cache, but we want to provide a shortcut to do this and collect
* telemetry on how often this is used.
*/
export function RestartServerButton({ error }: { error: Error }) {
const [showButton, setShowButton] = useState(false)
useEffect(() => {
const ERROR_KEY = `__next_error_overlay:${window.location.pathname}:${error.message}`
setShowButton(sessionStorage.getItem(ERROR_KEY) === '1')
// When the user tries to reload, set the error key to the session storage.
const handleBeforeUnload = () => {
sessionStorage.setItem(ERROR_KEY, '1')
}
window.addEventListener('beforeunload', handleBeforeUnload)
return () => {
window.removeEventListener('beforeunload', handleBeforeUnload)
}
}, [error.message])
export function RestartServerButton({ showButton }: { showButton: boolean }) {
if (!showButton) {
return null
}
......@@ -52,6 +40,50 @@ export function RestartServerButton({ error }: { error: Error }) {
)
}
/**
* Sets up a beforeunload listener to show the restart server button
* if the developer reloads on a specific error and that error persists with Turbopack + Persistent Cache.
*/
export function usePersistentCacheErrorDetection({
errors,
dispatch,
}: {
errors: SupportedErrorEvent[]
dispatch: OverlayDispatch
}) {
useEffect(() => {
const isTurbopackWithCache =
process.env.__NEXT_BUNDLER?.toUpperCase() === 'TURBOPACK' &&
process.env.__NEXT_BUNDLER_HAS_PERSISTENT_CACHE
// TODO: Is there a better heuristic here?
const firstError = errors[0]?.error
if (isTurbopackWithCache && firstError) {
const errorKey = `__next_error_overlay:${window.location.pathname}:${firstError.message}`
const showRestartServerButton = sessionStorage.getItem(errorKey) === '1'
dispatch({
type: ACTION_RESTART_SERVER_BUTTON,
showRestartServerButton,
})
const handleBeforeUnload = () => {
sessionStorage.setItem(errorKey, '1')
}
window.addEventListener('beforeunload', handleBeforeUnload)
return () => {
window.removeEventListener('beforeunload', handleBeforeUnload)
}
} else {
dispatch({
type: ACTION_RESTART_SERVER_BUTTON,
showRestartServerButton: false,
})
}
}, [errors, dispatch])
}
export const RESTART_SERVER_BUTTON_STYLES = `
.restart-dev-server-button {
-webkit-font-smoothing: antialiased;
......@@ -59,6 +91,7 @@ export const RESTART_SERVER_BUTTON_STYLES = `
justify-content: center;
align-items: center;
gap: 4px;
margin: 0 12px;
height: var(--size-26);
padding: 6px 8px 6px 6px;
......
import type { OverlayState } from '../../shared'
import type { OverlayDispatch } from '../../shared'
import { useMemo, useState, useEffect } from 'react'
import {
......@@ -7,6 +8,7 @@ import {
} from '../../utils/get-error-by-type'
import type { StackFrame } from 'next/dist/compiled/stacktrace-parser'
import type { ComponentStackFrame } from '../../utils/parse-component-stack'
import { usePersistentCacheErrorDetection } from '../../components/errors/error-overlay-toolbar/restart-server-button'
export type SupportedErrorEvent = {
id: number
......@@ -23,6 +25,7 @@ type Props = {
}) => React.ReactNode
state: OverlayState
isAppDir: boolean
dispatch: OverlayDispatch
}
export const RenderError = (props: Props) => {
......@@ -36,7 +39,7 @@ export const RenderError = (props: Props) => {
}
}
const RenderRuntimeError = ({ children, state, isAppDir }: Props) => {
const RenderRuntimeError = ({ children, state, isAppDir, dispatch }: Props) => {
const { errors } = state
const [lookups, setLookups] = useState<{
......@@ -65,6 +68,8 @@ const RenderRuntimeError = ({ children, state, isAppDir }: Props) => {
return [ready, next]
}, [errors, lookups])
usePersistentCacheErrorDetection({ errors, dispatch })
useEffect(() => {
if (nextError == null) {
return
......
......@@ -58,7 +58,7 @@ export function DevOverlay({
<ComponentStyles />
<DarkTheme />
<RenderError state={state} isAppDir={true}>
<RenderError state={state} dispatch={dispatch} isAppDir={true}>
{({ runtimeErrors, totalErrorCount }) => {
return (
<>
......
......@@ -36,6 +36,10 @@ export interface OverlayState {
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.
......@@ -76,6 +80,7 @@ export const ACTION_DEVTOOLS_PANEL_TOGGLE = 'devtools-panel-toggle'
export const ACTION_DEVTOOLS_POSITION = 'devtools-position'
export const ACTION_DEVTOOLS_SCALE = 'devtools-scale'
export const ACTION_RESTART_SERVER_BUTTON = 'restart-server-button'
export const STORAGE_KEY_THEME = '__nextjs-dev-tools-theme'
export const STORAGE_KEY_POSITION = '__nextjs-dev-tools-position'
......@@ -176,6 +181,11 @@ export interface DevToolUpdateRouteStateAction {
page: string
}
export interface RestartServerButtonAction {
type: typeof ACTION_RESTART_SERVER_BUTTON
showRestartServerButton: boolean
}
export type DispatcherEvent =
| BuildOkAction
| BuildErrorAction
......@@ -200,6 +210,8 @@ export type DispatcherEvent =
| DevToolsIndicatorPositionAction
| DevToolsScaleAction
| DevToolUpdateRouteStateAction
| RestartServerButtonAction
const REACT_ERROR_STACK_BOTTOM_FRAME_REGEX =
// 1st group: v8
// 2nd group: SpiderMonkey, JavaScriptCore
......@@ -238,6 +250,7 @@ export const INITIAL_OVERLAY_STATE: Omit<
versionInfo: { installed: '0.0.0', staleness: 'unknown' },
debugInfo: { devtoolsFrontendUrl: undefined },
isDevToolsPanelOpen: false,
showRestartServerButton: false,
devToolsPosition: 'bottom-left',
scale: NEXT_DEV_TOOLS_SCALE.Medium,
page: '',
......@@ -421,6 +434,12 @@ export function useErrorOverlayReducer(
case ACTION_DEVTOOL_UPDATE_ROUTE_STATE: {
return { ...state, page: action.page }
}
case ACTION_RESTART_SERVER_BUTTON: {
return {
...state,
showRestartServerButton: action.showRestartServerButton,
}
}
default: {
return state
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment