Unverified Commit 96b31f8e authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

[devtool] fix duplicate rendered indicator on server (#81729)

There're should be only one indicator rendered when `showIndicator` state is true, the previous condition is not correct since the previous change seem causing indicator always rendered even on SSR. since devtool bundle is also bundled on ssr layer, but the related code should not get executed. We noticed the previous condition will always render the indicator, which likely triggered some `base-ui` browser code to execute on server and crash on windows.

Related #81722
parent 4e0b1125
[data-nextjs-toast] {
&[data-hidden='true'] {
display: none;
}
}
.dev-tools-indicator-menu {
-webkit-font-smoothing: antialiased;
display: flex;
flex-direction: column;
align-items: flex-start;
background: var(--color-background-100);
border: 1px solid var(--color-gray-alpha-400);
background-clip: padding-box;
box-shadow: var(--shadow-menu);
border-radius: var(--rounded-xl);
position: absolute;
font-family: var(--font-stack-sans);
z-index: 3;
overflow: hidden;
opacity: 0;
outline: 0;
min-width: 248px;
transition: opacity var(--animate-out-duration-ms)
var(--animate-out-timing-function);
&[data-rendered='true'] {
opacity: 1;
scale: 1;
}
}
.dev-tools-indicator-inner {
padding: 6px;
width: 100%;
}
.dev-tools-indicator-item {
display: flex;
align-items: center;
padding: 8px 6px;
height: var(--size-36);
border-radius: 6px;
text-decoration: none !important;
user-select: none;
white-space: nowrap;
svg {
width: var(--size-16);
height: var(--size-16);
}
&:focus-visible {
outline: 0;
}
}
.dev-tools-indicator-footer {
background: var(--color-background-200);
padding: 6px;
border-top: 1px solid var(--color-gray-400);
width: 100%;
}
.dev-tools-indicator-item[data-selected='true'] {
cursor: pointer;
background-color: var(--color-gray-200);
}
.dev-tools-indicator-label {
font-size: var(--size-14);
line-height: var(--size-20);
color: var(--color-gray-1000);
}
.dev-tools-indicator-value {
font-size: var(--size-14);
line-height: var(--size-20);
color: var(--color-gray-900);
margin-left: auto;
}
.dev-tools-indicator-issue-count {
--color-primary: var(--color-gray-800);
--color-secondary: var(--color-gray-100);
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
gap: 8px;
min-width: var(--size-40);
height: var(--size-24);
background: var(--color-background-100);
border: 1px solid var(--color-gray-alpha-400);
background-clip: padding-box;
box-shadow: var(--shadow-small);
padding: 2px;
color: var(--color-gray-1000);
border-radius: 128px;
font-weight: 500;
font-size: var(--size-13);
font-variant-numeric: tabular-nums;
&[data-has-issues='true'] {
--color-primary: var(--color-red-800);
--color-secondary: var(--color-red-100);
}
.dev-tools-indicator-issue-count-indicator {
width: var(--size-8);
height: var(--size-8);
background: var(--color-primary);
box-shadow: 0 0 0 2px var(--color-secondary);
border-radius: 50%;
}
}
.dev-tools-indicator-shortcut {
display: flex;
gap: 4px;
kbd {
width: var(--size-20);
height: var(--size-20);
display: flex;
justify-content: center;
align-items: center;
border-radius: var(--rounded-md);
border: 1px solid var(--color-gray-400);
font-family: var(--font-stack-sans);
background: var(--color-background-100);
color: var(--color-gray-1000);
text-align: center;
font-size: var(--size-12);
line-height: var(--size-16);
}
}
.dev-tools-grabbing {
cursor: grabbing;
> * {
pointer-events: none;
}
}
import type { Meta, StoryObj } from '@storybook/react' import type { Meta, StoryObj } from '@storybook/react'
import type { OverlayState } from '../../shared' import type { OverlayState } from '../../shared'
import { DevToolsIndicatorNew } from './devtools-indicator' import { DevToolsIndicator } from './devtools-indicator'
import { INITIAL_OVERLAY_STATE } from '../../shared' import { INITIAL_OVERLAY_STATE } from '../../shared'
import { withShadowPortal } from '../../storybook/with-shadow-portal' import { withShadowPortal } from '../../storybook/with-shadow-portal'
const meta: Meta<typeof DevToolsIndicatorNew> = { const meta: Meta<typeof DevToolsIndicator> = {
component: DevToolsIndicatorNew, component: DevToolsIndicator,
parameters: { parameters: {
layout: 'centered', layout: 'centered',
}, },
...@@ -37,7 +37,7 @@ const meta: Meta<typeof DevToolsIndicatorNew> = { ...@@ -37,7 +37,7 @@ const meta: Meta<typeof DevToolsIndicatorNew> = {
} }
export default meta export default meta
type Story = StoryObj<typeof DevToolsIndicatorNew> type Story = StoryObj<typeof DevToolsIndicator>
const state: OverlayState = { const state: OverlayState = {
...INITIAL_OVERLAY_STATE, ...INITIAL_OVERLAY_STATE,
......
import './devtools-indicator.css'
import type { CSSProperties } from 'react' import type { CSSProperties } from 'react'
import { NextLogo } from './next-logo'
import { NextLogoNew } from './next-logo'
import { Toast } from '../toast' import { Toast } from '../toast'
import { import {
MENU_CURVE, MENU_CURVE,
...@@ -20,7 +20,7 @@ import type { DevToolsIndicatorPosition } from '../errors/dev-tools-indicator/de ...@@ -20,7 +20,7 @@ import type { DevToolsIndicatorPosition } from '../errors/dev-tools-indicator/de
export const INDICATOR_PADDING = 20 export const INDICATOR_PADDING = 20
export function DevToolsIndicatorNew() { export function DevToolsIndicator() {
const { state, dispatch } = useDevOverlayContext() const { state, dispatch } = useDevOverlayContext()
const { panel, setPanel, setSelectedIndex } = usePanelRouterContext() const { panel, setPanel, setSelectedIndex } = usePanelRouterContext()
const updateAllPanelPositions = useUpdateAllPanelPositions() const updateAllPanelPositions = useUpdateAllPanelPositions()
...@@ -56,7 +56,7 @@ export function DevToolsIndicatorNew() { ...@@ -56,7 +56,7 @@ export function DevToolsIndicatorNew() {
updateAllPanelPositions(p) updateAllPanelPositions(p)
}} }}
> >
<NextLogoNew <NextLogo
onTriggerClick={() => { onTriggerClick={() => {
const newPanel = const newPanel =
panel === 'panel-selector' ? null : 'panel-selector' panel === 'panel-selector' ? null : 'panel-selector'
......
import type { Meta, StoryObj } from '@storybook/react' import type { Meta, StoryObj } from '@storybook/react'
import { NextLogoNew } from './next-logo' import { NextLogo } from './next-logo'
import { withShadowPortal } from '../../storybook/with-shadow-portal' import { withShadowPortal } from '../../storybook/with-shadow-portal'
// TODO(rob): this storybook's configurability is broken intentionally // TODO(rob): this storybook's configurability is broken intentionally
// it will be restored when we wrap it in the storybook context addon https://storybook.js.org/addons/storybook-react-context // it will be restored when we wrap it in the storybook context addon https://storybook.js.org/addons/storybook-react-context
...@@ -10,7 +10,7 @@ const StoryBookNextLogo = ( ...@@ -10,7 +10,7 @@ const StoryBookNextLogo = (
isDevRendering: boolean isDevRendering: boolean
} & Record<string, unknown> } & Record<string, unknown>
) => { ) => {
return <NextLogoNew onTriggerClick={() => {}} /> return <NextLogo onTriggerClick={() => {}} />
} }
const meta: Meta<typeof StoryBookNextLogo> = { const meta: Meta<typeof StoryBookNextLogo> = {
......
...@@ -16,7 +16,7 @@ import { BASE_LOGO_SIZE } from '../../utils/indicator-metrics' ...@@ -16,7 +16,7 @@ import { BASE_LOGO_SIZE } from '../../utils/indicator-metrics'
const SHORT_DURATION_MS = 150 const SHORT_DURATION_MS = 150
export function NextLogoNew({ export function NextLogo({
onTriggerClick, onTriggerClick,
...buttonProps ...buttonProps
}: { onTriggerClick: () => void } & React.ComponentProps<'button'>) { }: { onTriggerClick: () => void } & React.ComponentProps<'button'>) {
......
import type { Meta, StoryObj } from '@storybook/react'
import { DevOverlayStoryWrapper } from '../../../storybook/DevOverlayStoryWrapper'
import { withShadowPortal } from '../../../storybook/with-shadow-portal'
import type { VersionInfo } from '../../../../../server/dev/parse-version-info'
import {
getStoredPanelPosition,
STORE_KEY_SHARED_PANEL_LOCATION,
type OverlayState,
} from '../../../shared'
const meta: Meta<typeof DevOverlayStoryWrapper> = {
component: DevOverlayStoryWrapper,
parameters: {
layout: 'centered',
},
argTypes: {},
decorators: [
withShadowPortal,
// Test for high z-index
(Story) => (
<div
style={{
position: 'fixed',
top: 0,
left: 0,
width: '100%',
height: '100%',
zIndex: 9999,
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
background:
'linear-gradient(135deg, rgba(230,240,255,0.8) 0%, rgba(200,220,255,0.6) 100%)',
}}
>
<Story />
</div>
),
],
}
export default meta
type Story = StoryObj<typeof DevOverlayStoryWrapper>
// Mock version info for stories
const mockVersionInfo: VersionInfo = {
installed: '15.1.2',
staleness: 'stale-major',
}
const state: OverlayState = {
routerType: 'app',
devToolsPanelPosition: {
[STORE_KEY_SHARED_PANEL_LOCATION]: getStoredPanelPosition(),
},
nextId: 1,
buildError: null,
errors: [],
refreshState: { type: 'idle' },
disableDevIndicator: false,
showIndicator: true,
versionInfo: mockVersionInfo,
notFound: false,
buildingIndicator: false,
renderingIndicator: false,
staticIndicator: true,
debugInfo: { devtoolsFrontendUrl: undefined },
isErrorOverlayOpen: false,
showRestartServerButton: false,
devToolsPosition: 'bottom-left',
scale: 1,
page: '',
}
export const StaticRoute: Story = {
render: () => (
<DevOverlayStoryWrapper initialState={state} runtimeErrors={[]} />
),
}
export const DynamicRoute: Story = {
render: () => (
<DevOverlayStoryWrapper
initialState={{ ...state, staticIndicator: false }}
runtimeErrors={[]}
/>
),
}
export const SingleError: Story = {
render: () => (
<DevOverlayStoryWrapper initialState={state} runtimeErrors={[{} as any]} />
),
}
export const MultipleErrors: Story = {
render: () => (
<DevOverlayStoryWrapper
initialState={state}
runtimeErrors={[{}, {}, {}] as any}
/>
),
}
import { PageSegmentTree } from '../../../overview/segment-explorer'
import { DevToolsInfo, type DevToolsInfoPropsCore } from './dev-tools-info'
export function SegmentsExplorer({
routerType,
page,
...props
}: DevToolsInfoPropsCore &
React.HTMLProps<HTMLDivElement> & {
routerType: 'app' | 'pages'
page: string
}) {
const isAppRouter = routerType === 'app'
return (
<DevToolsInfo title="Route Info" {...props}>
<div data-nextjs-segments-explorer style={{ margin: '-16px' }}>
<PageSegmentTree isAppRouter={isAppRouter} page={page} />
</div>
</DevToolsInfo>
)
}
import type { Meta, StoryObj } from '@storybook/react'
import { NextLogo } from './next-logo'
import { withShadowPortal } from '../../../storybook/with-shadow-portal'
const meta: Meta<typeof NextLogo> = {
component: NextLogo,
parameters: {
layout: 'centered',
},
args: {
'aria-label': 'Open Next.js DevTools',
onClick: () => console.log('Clicked!'),
},
decorators: [withShadowPortal],
}
export default meta
type Story = StoryObj<typeof NextLogo>
export const NoIssues: Story = {
args: {
issueCount: 0,
isDevBuilding: false,
isDevRendering: false,
},
}
export const SingleIssue: Story = {
args: {
issueCount: 1,
isDevBuilding: false,
isDevRendering: false,
},
}
export const MultipleIssues: Story = {
args: {
issueCount: 5,
isDevBuilding: false,
isDevRendering: false,
},
}
export const ManyIssues: Story = {
args: {
issueCount: 99,
isDevBuilding: false,
isDevRendering: false,
},
}
export const Building: Story = {
args: {
issueCount: 0,
isDevBuilding: true,
isDevRendering: false,
},
}
export const BuildingWithError: Story = {
args: {
issueCount: 1,
isDevBuilding: true,
isDevRendering: false,
},
}
export const Rendering: Story = {
args: {
issueCount: 0,
isDevBuilding: false,
isDevRendering: true,
},
}
export const RenderingWithError: Story = {
args: {
issueCount: 1,
isDevBuilding: false,
isDevRendering: true,
},
}
...@@ -5,13 +5,11 @@ import { ComponentStyles } from './styles/component-styles' ...@@ -5,13 +5,11 @@ import { ComponentStyles } from './styles/component-styles'
import { CssReset } from './styles/css-reset' import { CssReset } from './styles/css-reset'
import { Colors } from './styles/colors' import { Colors } from './styles/colors'
import { ErrorOverlay } from './components/errors/error-overlay/error-overlay' import { ErrorOverlay } from './components/errors/error-overlay/error-overlay'
import { DevToolsIndicator } from './components/errors/dev-tools-indicator/dev-tools-indicator'
import { RenderError } from './container/runtime-error/render-error' import { RenderError } from './container/runtime-error/render-error'
import { DarkTheme } from './styles/dark-theme' import { DarkTheme } from './styles/dark-theme'
import { useDevToolsScale } from './components/errors/dev-tools-indicator/dev-tools-info/preferences'
import type { HydrationErrorState } from '../shared/hydration-error' import type { HydrationErrorState } from '../shared/hydration-error'
import type { ReadyRuntimeError } from './utils/get-error-by-type' import type { ReadyRuntimeError } from './utils/get-error-by-type'
import { DevToolsIndicatorNew } from './components/devtools-indicator/devtools-indicator' import { DevToolsIndicator } from './components/devtools-indicator/devtools-indicator'
import { PanelRouter } from './menu/panel-router' import { PanelRouter } from './menu/panel-router'
import { PanelRouterContext, type PanelStateKind } from './menu/context' import { PanelRouterContext, type PanelStateKind } from './menu/context'
import type { OverlayState, OverlayDispatch } from './shared' import type { OverlayState, OverlayDispatch } from './shared'
...@@ -32,9 +30,7 @@ export function DevOverlay({ ...@@ -32,9 +30,7 @@ export function DevOverlay({
dispatch: OverlayDispatch dispatch: OverlayDispatch
getSquashedHydrationErrorDetails: (error: Error) => HydrationErrorState | null getSquashedHydrationErrorDetails: (error: Error) => HydrationErrorState | null
}) { }) {
const [scale, setScale] = useDevToolsScale()
const [panel, setPanel] = useState<null | PanelStateKind>(null) const [panel, setPanel] = useState<null | PanelStateKind>(null)
const isBuildError = state.buildError !== null
const [selectedIndex, setSelectedIndex] = useState(-1) const [selectedIndex, setSelectedIndex] = useState(-1)
const triggerRef = useRef<HTMLButtonElement>(null) const triggerRef = useRef<HTMLButtonElement>(null)
...@@ -74,32 +70,11 @@ export function DevOverlay({ ...@@ -74,32 +70,11 @@ export function DevOverlay({
errorCount={totalErrorCount} errorCount={totalErrorCount}
/> />
<PanelRouter /> <PanelRouter />
<DevToolsIndicatorNew /> <DevToolsIndicator />
</PanelRouterContext> </PanelRouterContext>
</RenderErrorContext> </RenderErrorContext>
</> </>
) : ( ) : null}
<>
<DevToolsIndicator
scale={scale}
setScale={setScale}
state={state}
dispatch={dispatch}
errorCount={totalErrorCount}
isBuildError={isBuildError}
/>
<ErrorOverlay
state={state}
dispatch={dispatch}
getSquashedHydrationErrorDetails={
getSquashedHydrationErrorDetails
}
runtimeErrors={runtimeErrors}
errorCount={totalErrorCount}
/>
</>
)}
</> </>
) )
}} }}
......
import { useDevOverlayContext } from '../../dev-overlay.browser' import { useDevOverlayContext } from '../../dev-overlay.browser'
import { useClickOutsideAndEscape } from '../components/errors/dev-tools-indicator/utils' import { useClickOutsideAndEscape } from '../components/errors/dev-tools-indicator/utils'
import { useLayoutEffect, useRef, type CSSProperties } from 'react' import {
useLayoutEffect,
useRef,
createContext,
useContext,
type CSSProperties,
type Dispatch,
type SetStateAction,
} from 'react'
import { getIndicatorOffset } from '../utils/indicator-metrics' import { getIndicatorOffset } from '../utils/indicator-metrics'
import { INDICATOR_PADDING } from '../components/devtools-indicator/devtools-indicator' import { INDICATOR_PADDING } from '../components/devtools-indicator/devtools-indicator'
import {
MenuContext,
MenuItem,
} from '../components/errors/dev-tools-indicator/dev-tools-indicator'
import { usePanelRouterContext } from './context' import { usePanelRouterContext } from './context'
import { usePanelContext } from './panel-router' import { usePanelContext } from './panel-router'
interface C {
closeMenu?: () => void
selectedIndex: number
setSelectedIndex: Dispatch<SetStateAction<number>>
}
export const MenuContext = createContext({} as C)
export function MenuItem({
index,
label,
value,
onClick,
href,
...props
}: {
index?: number
title?: string
label: string
value: React.ReactNode
href?: string
onClick?: () => void
}) {
const isInteractive =
typeof onClick === 'function' || typeof href === 'string'
const { closeMenu, selectedIndex, setSelectedIndex } = useContext(MenuContext)
const selected = selectedIndex === index
function click() {
if (isInteractive) {
onClick?.()
closeMenu?.()
if (href) {
window.open(href, '_blank', 'noopener, noreferrer')
}
}
}
return (
<div
className="dev-tools-indicator-item"
data-index={index}
data-selected={selected}
onClick={click}
// Needs `onMouseMove` instead of enter to work together
// with keyboard and mouse input
onMouseMove={() => {
if (isInteractive && index !== undefined && selectedIndex !== index) {
setSelectedIndex(index)
}
}}
onMouseLeave={() => setSelectedIndex(-1)}
onKeyDown={(e) => {
if (e.key === 'Enter' || e.key === ' ') {
click()
}
}}
role={isInteractive ? 'menuitem' : undefined}
tabIndex={selected ? 0 : -1}
{...props}
>
<span className="dev-tools-indicator-label">{label}</span>
<span className="dev-tools-indicator-value">{value}</span>
</div>
)
}
export const DevtoolMenu = ({ export const DevtoolMenu = ({
closeOnClickOutside = true, closeOnClickOutside = true,
items, items,
......
...@@ -12,7 +12,6 @@ import { styles as containerErrorStyles } from '../container/errors' ...@@ -12,7 +12,6 @@ import { styles as containerErrorStyles } from '../container/errors'
import { styles as containerRuntimeErrorStyles } from '../container/runtime-error' import { styles as containerRuntimeErrorStyles } from '../container/runtime-error'
import { COPY_BUTTON_STYLES } from '../components/copy-button' import { COPY_BUTTON_STYLES } from '../components/copy-button'
import { CALL_STACK_FRAME_STYLES } from '../components/call-stack-frame/call-stack-frame' import { CALL_STACK_FRAME_STYLES } from '../components/call-stack-frame/call-stack-frame'
import { DEV_TOOLS_INDICATOR_STYLES } from '../components/errors/dev-tools-indicator/dev-tools-indicator'
import { css } from '../utils/css' import { css } from '../utils/css'
import { EDITOR_LINK_STYLES } from '../components/terminal/editor-link' import { EDITOR_LINK_STYLES } from '../components/terminal/editor-link'
import { ENVIRONMENT_NAME_LABEL_STYLES } from '../components/errors/environment-name-label/environment-name-label' import { ENVIRONMENT_NAME_LABEL_STYLES } from '../components/errors/environment-name-label/environment-name-label'
...@@ -48,7 +47,6 @@ export function ComponentStyles() { ...@@ -48,7 +47,6 @@ export function ComponentStyles() {
${containerRuntimeErrorStyles} ${containerRuntimeErrorStyles}
${versionStaleness} ${versionStaleness}
${RESTART_SERVER_BUTTON_STYLES} ${RESTART_SERVER_BUTTON_STYLES}
${DEV_TOOLS_INDICATOR_STYLES}
${DEV_TOOLS_INFO_STYLES} ${DEV_TOOLS_INFO_STYLES}
${DEV_TOOLS_INFO_TURBOPACK_INFO_STYLES} ${DEV_TOOLS_INFO_TURBOPACK_INFO_STYLES}
${DEV_TOOLS_INFO_ROUTE_INFO_STYLES} ${DEV_TOOLS_INFO_ROUTE_INFO_STYLES}
......
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