Unverified Commit f260d466 authored by Will Binns-Smith's avatar Will Binns-Smith Committed by GitHub

Use Next.js conventions in bundle analyzer UI (#98780)

Use the App Router error boundary and `next-themes` provider instead of
custom client-side equivalents.

Persist environment, file type, and debounced file search filters in URL
search parameters so analyzer views can be shared. Remove the
incompatible `next start` script from the statically exported app.

## What?

- Add an App Router `error.tsx` boundary with segment reset support
- Replace the inline theme bootstrap script with `next-themes`
- Store analyzer filters and file search in URL search parameters
- Remove the static export's `next start` script

## Why?

Use the framework's existing error, theme, and navigation integration
while keeping the analyzer's static export deployment model.

## How was it tested?

- `pnpm --filter=@next/bundle-analyzer-ui exec tsc --noEmit`
- `pnpm --filter=@next/bundle-analyzer-ui lint`
- `pnpm --filter=@next/bundle-analyzer-ui build`

<!-- NEXT_JS_LLM -->
Co-authored-by: 's avatarvercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com>
Co-authored-by: 's avatarWill Binns-Smith <755844+wbinnssmith@users.noreply.github.com>
parent ec4225cd
'use client'
import { ErrorState } from '@/components/error-state'
import { useEffect } from 'react'
export default function Error({
error,
reset,
}: {
error: Error & { digest?: string }
reset: () => void
}) {
useEffect(() => {
console.error(error)
}, [error])
return (
<main className="h-screen flex bg-background">
<ErrorState error={error} onRetry={reset} />
</main>
)
}
import type { Metadata } from 'next'
import type React from 'react'
import { ThemeProvider } from '@/components/theme-provider'
import './globals.css'
export const metadata: Metadata = {
......@@ -14,31 +15,12 @@ export default function RootLayout({
children: React.ReactNode
}>) {
return (
<html
lang="en"
// Suppress hydration warnings here specifically because we'll modify the
// classname for the theme before React hydates. This is to prevent a flash
// of incorrect theme.
suppressHydrationWarning
>
<head>
<script
// biome-ignore lint/security/noDangerouslySetInnerHtml: Needed to prevent flash of incorrect theme
dangerouslySetInnerHTML={{
__html: `
(function() {
const theme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
document.documentElement.classList.toggle('dark', theme === 'dark');
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', (e) => {
document.documentElement.classList.toggle('dark', e.matches);
});
})();
`,
}}
/>
</head>
<body className="font-sans antialiased">{children}</body>
<html lang="en" suppressHydrationWarning>
<body className="font-sans antialiased">
<ThemeProvider attribute="class" defaultTheme="system" enableSystem>
{children}
</ThemeProvider>
</body>
</html>
)
}
'use client'
import {
Component,
Suspense,
useEffect,
useMemo,
useRef,
useState,
type ErrorInfo,
type ReactNode,
} from 'react'
import Link from 'next/link'
......@@ -18,7 +16,6 @@ import {
type CompareLayoutModel,
} from '@/components/compare-layout'
import { DiffTable } from '@/components/diff-table'
import { ErrorState } from '@/components/error-state'
import { Sidebar } from '@/components/sidebar'
import { TopBar, Environment, CompareView } from '@/components/top-bar'
import { TreemapVisualizer } from '@/components/treemap-visualizer'
......@@ -80,40 +77,12 @@ function AnalyzerBoundary({
defaultView: CompareView
}) {
return (
<AnalyzerErrorBoundary>
<Suspense fallback={<AnalyzerFallback view={defaultView} />}>
{children}
</Suspense>
</AnalyzerErrorBoundary>
<Suspense fallback={<AnalyzerFallback view={defaultView} />}>
{children}
</Suspense>
)
}
class AnalyzerErrorBoundary extends Component<
{ children: ReactNode },
{ error: unknown }
> {
state: { error: unknown } = { error: null }
static getDerivedStateFromError(error: unknown) {
return { error }
}
componentDidCatch(error: unknown, info: ErrorInfo) {
console.error(error, info.componentStack)
}
render() {
if (this.state.error) {
return (
<main className="h-screen bg-background">
<ErrorState error={this.state.error} />
</main>
)
}
return this.props.children
}
}
function AnalyzerFallback({ view }: { view: CompareView }) {
return <AnalyzerChromeSkeleton view={view} />
}
......
......@@ -8,7 +8,7 @@ interface ErrorStateProps {
onRetry?: () => void
}
export function ErrorState({ error }: ErrorStateProps) {
export function ErrorState({ error, onRetry }: ErrorStateProps) {
const isNetwork = error instanceof NetworkError
const title = isNetwork ? 'Server Connection Lost' : 'Error'
const message = isNetwork
......@@ -27,9 +27,7 @@ export function ErrorState({ error }: ErrorStateProps) {
</h3>
<p className="text-sm text-muted-foreground mb-4">{message}</p>
<button
onClick={() => {
window.location.reload()
}}
onClick={onRetry ?? (() => window.location.reload())}
className="inline-flex items-center gap-2 px-4 py-2 rounded-md bg-primary text-primary-foreground hover:bg-primary/90 transition-colors text-sm font-medium"
>
<RefreshCw className="w-4 h-4" />
......
......@@ -5,8 +5,7 @@
"scripts": {
"build": "cross-env NEXT_TEST_NATIVE_IGNORE_LOCAL_INSTALL=true NEXT_TEST_WASM= next build --no-mangling",
"dev": "cross-env NEXT_TEST_NATIVE_IGNORE_LOCAL_INSTALL=true next dev",
"lint": "eslint .",
"start": "cross-env NEXT_TEST_NATIVE_IGNORE_LOCAL_INSTALL=true next start"
"lint": "eslint ."
},
"dependencies": {
"@radix-ui/react-dialog": "1.1.4",
......
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