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

bundle-analyzer: suspend while loading analyzer data (#98535)

## Summary

Use SWR Suspense for required analyzer datasets and move loading
behavior into explicit Suspense and error boundaries.

Split selected-route and valid-comparison subtrees so resolved route and
module data has non-null contracts. Keep optional history, metadata, and
one-sided comparison data nullable where absence is part of the UI
state.


## Verification

- `pnpm --filter=@next/bundle-analyzer-ui exec tsc --noEmit`
- focused ESLint for changed files
- `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 6137b367
import { Suspense } from 'react'
import { CompareAnalyzer } from '@/components/analyzer'
export default function ComparePage() {
return (
<Suspense>
<CompareAnalyzer />
</Suspense>
)
return <CompareAnalyzer />
}
import { Suspense } from 'react'
import { SingleAnalyzer } from '@/components/analyzer'
export default function HomePage() {
return (
<Suspense>
<SingleAnalyzer />
</Suspense>
)
return <SingleAnalyzer />
}
......@@ -4,9 +4,9 @@ import type { MouseEventHandler } from 'react'
import { CompareSidebar } from '@/components/sidebar'
import { DiffTable } from '@/components/diff-table'
import { DiffTreemap } from '@/components/diff-treemap'
import { TreemapSkeleton } from '@/components/ui/skeleton'
import { StatCard, CountCard } from '@/components/stat-cards'
import { CompareView, Environment } from '@/components/top-bar'
import { TableSkeleton, TreemapSkeleton } from '@/components/ui/skeleton'
import { AnalyzeData, ModulesData } from '@/lib/analyze-data'
import {
diffRoutesWithSizes,
......@@ -31,15 +31,14 @@ export interface CompareLayoutModel {
sourceDiff: DiffSummary<SourceDiffRow> | null
analyzeData: AnalyzeData | null
baselineAnalyzeData: AnalyzeData | null
modulesData: ModulesData | null
baselineModulesData: ModulesData | null
modulesData: ModulesData
baselineModulesData: ModulesData
moduleDepthMap: Map<number, number>
baselineModuleDepthMap: Map<number, number>
environmentFilter: Environment
sidebarWidth: number
compareView: CompareView
isAnalyzeLoading: boolean
isBaselineAnalyzeLoading: boolean
isViewPending: boolean
searchQuery: string
selectedKey: string | null
onSelectedKeyChange: (key: string | null) => void
......@@ -60,8 +59,6 @@ export function CompareLayout({ model, onResizeSidebar }: CompareLayoutProps) {
<RouteStatsCard
selectedRoute={model.selectedRoute}
sourceDiff={model.sourceDiff}
isAnalyzeLoading={model.isAnalyzeLoading}
isBaselineAnalyzeLoading={model.isBaselineAnalyzeLoading}
compressed
className="xl:min-w-0 xl:flex-1 xl:basis-0 xl:border-r xl:border-border"
/>
......@@ -77,12 +74,11 @@ export function CompareLayout({ model, onResizeSidebar }: CompareLayoutProps) {
<div className="flex flex-1 min-w-0 flex-col">
<ComparePerRoutePanel
compareView={model.compareView}
isViewPending={model.isViewPending}
selectedRoute={model.selectedRoute}
sourceDiff={model.sourceDiff}
analyzeData={model.analyzeData}
baselineAnalyzeData={model.baselineAnalyzeData}
isAnalyzeLoading={model.isAnalyzeLoading}
isBaselineAnalyzeLoading={model.isBaselineAnalyzeLoading}
compressed
searchQuery={model.searchQuery}
baselineSnapshot={model.baselineSnapshot}
......@@ -225,26 +221,18 @@ export function CompareContextStrip({
export function RouteStatsCard({
selectedRoute,
sourceDiff,
isAnalyzeLoading,
isBaselineAnalyzeLoading,
compressed,
className,
}: {
selectedRoute: string | null
sourceDiff: DiffSummary<SourceDiffRow> | null
isAnalyzeLoading: boolean
isBaselineAnalyzeLoading: boolean
compressed: boolean
className?: string
}) {
const isLoading = isAnalyzeLoading || isBaselineAnalyzeLoading
const body = !selectedRoute ? (
<div className="pt-1 text-xs text-muted-foreground">
Select a route above to see per-source changes.
</div>
) : isLoading ? (
<div className="pt-1 text-xs text-muted-foreground">Loading…</div>
) : !sourceDiff ? (
<div className="pt-1 text-xs text-muted-foreground">
No data for this route.
......@@ -339,12 +327,11 @@ function RouteStatsBody({
*/
export function ComparePerRoutePanel({
compareView,
isViewPending,
selectedRoute,
sourceDiff,
analyzeData,
baselineAnalyzeData,
isAnalyzeLoading,
isBaselineAnalyzeLoading,
compressed,
searchQuery,
baselineSnapshot,
......@@ -353,12 +340,11 @@ export function ComparePerRoutePanel({
onCompareSelectedKeyChange,
}: {
compareView: CompareView
isViewPending: boolean
selectedRoute: string | null
sourceDiff: DiffSummary<SourceDiffRow> | null
analyzeData: AnalyzeData | null
baselineAnalyzeData: AnalyzeData | null
isAnalyzeLoading: boolean
isBaselineAnalyzeLoading: boolean
compressed: boolean
searchQuery: string
baselineSnapshot: SnapshotMetadata
......@@ -373,17 +359,26 @@ export function ComparePerRoutePanel({
</div>
)
}
if (isAnalyzeLoading || isBaselineAnalyzeLoading) {
if (!sourceDiff) {
return (
<div className="p-4">
<TreemapSkeleton />
<div className="flex flex-1 items-center justify-center p-4 text-sm text-muted-foreground">
No data for this route.
</div>
)
}
if (!sourceDiff) {
if (isViewPending) {
return (
<div className="flex flex-1 items-center justify-center p-4 text-sm text-muted-foreground">
No data for this route.
<div
className="flex flex-1 min-h-0 p-4"
role="status"
aria-label="Loading view"
>
{compareView === CompareView.Treemap ? (
<TreemapSkeleton />
) : (
<TableSkeleton />
)}
</div>
)
}
......
'use client'
import useSWR from 'swr'
import { Check, ChevronsUpDown, Loader, Route } from 'lucide-react'
import { Check, ChevronsUpDown, Route } from 'lucide-react'
import { useEffect, useMemo, useState } from 'react'
import { Button } from '@/components/ui/button'
import { useSuspenseJsonData } from '@/lib/analyzer-data'
import {
Command,
CommandEmpty,
......@@ -17,8 +17,7 @@ import {
PopoverContent,
PopoverTrigger,
} from '@/components/ui/popover'
import { cn, jsonFetcher } from '@/lib/utils'
import { NetworkError } from '@/lib/errors'
import { cn } from '@/lib/utils'
import { Kbd } from '@/components/ui/kbd'
import {
delta,
......@@ -76,11 +75,7 @@ export function RouteTypeahead({
return () => window.removeEventListener('keydown', handleKeyDown)
}, [])
const {
data: routes,
isLoading,
error,
} = useSWR<string[]>('/data/routes.json', jsonFetcher, {
const routes = useSuspenseJsonData<string[]>('/data/routes.json', {
onSuccess: (routeNames) => {
// Auto-select first route if none is selected
if (routeNames.length > 0 && selectedRoute == null) {
......@@ -100,7 +95,7 @@ export function RouteTypeahead({
row,
}))
}
return (routes ?? []).map((name) => ({ name, row: null }))
return routes.map((name) => ({ name, row: null }))
}, [routes, routeDiff, useCompressed])
// Find the currently selected route's diff row, used to render a delta
......@@ -110,27 +105,7 @@ export function RouteTypeahead({
return routeDiff.rows.find((r) => r.key === selectedRoute) ?? null
}, [routeDiff, selectedRoute])
if (error) {
return (
<div className="flex items-center gap-2 px-3 py-2 rounded-md bg-destructive/10 border border-destructive/20 text-destructive text-sm max-w-full">
<span className="font-medium">⚠</span>
<span className="truncate">
{error instanceof NetworkError
? 'Unable to connect to server'
: error.message}
</span>
</div>
)
}
let ctaText: React.ReactNode
if (isLoading) {
ctaText = 'Loading routes...'
} else if (selectedRoute != null) {
ctaText = selectedRoute
} else {
ctaText = 'Select route...'
}
const ctaText = selectedRoute ?? 'Select route...'
return (
<div className="flex items-center gap-2 min-w-64 max-w-full">
......@@ -140,15 +115,10 @@ export function RouteTypeahead({
variant="outline"
role="combobox"
aria-expanded={open}
disabled={isLoading}
className="flex-grow-1 w-full justify-between font-mono text-sm"
>
<div className="flex items-center min-w-0">
{isLoading ? (
<Loader className="mr-2 inline animate-spin" />
) : (
<Route className="inline mr-2 shrink-0" />
)}
<Route className="inline mr-2 shrink-0" />
<span className="truncate">{ctaText}</span>
{selectedRow ? (
......
......@@ -10,7 +10,6 @@ import {
TooltipTrigger,
} from './ui/tooltip'
import { ImportChain } from '@/components/import-chain'
import { Skeleton } from '@/components/ui/skeleton'
import { AnalyzeData, ModulesData } from '@/lib/analyze-data'
import { SpecialModule } from '@/lib/types'
import { cn, getSpecialModuleType } from '@/lib/utils'
......@@ -20,13 +19,12 @@ import { formatDelta } from '@/lib/diff'
interface SidebarProps {
sidebarWidth: number
analyzeData: AnalyzeData | null
modulesData: ModulesData | null
analyzeData: AnalyzeData
modulesData: ModulesData
selectedSourceIndex: number | null
moduleDepthMap: Map<number, number>
environmentFilter: 'client' | 'server'
filterSource?: (sourceIndex: number) => boolean
isLoading?: boolean
}
function formatBytes(bytes: number): string {
......@@ -46,30 +44,9 @@ export function Sidebar({
moduleDepthMap,
environmentFilter,
filterSource,
isLoading = false,
}: SidebarProps) {
filterSource = filterSource ?? (() => true)
if (isLoading || !analyzeData) {
return (
<div
className="flex-none bg-muted border-l border-border overflow-y-auto"
style={{ width: `${sidebarWidth}%` }}
>
<div className="flex-1 p-3 space-y-4 overflow-y-auto">
<Skeleton className="h-4 w-3/4" />
<Skeleton className="h-4 w-full" />
<Skeleton className="h-4 w-5/6" />
<div className="mt-4 space-y-2">
<Skeleton className="h-3 w-full" />
<Skeleton className="h-3 w-full" />
<Skeleton className="h-3 w-4/5" />
</div>
</div>
</div>
)
}
return (
<div
className="flex-none bg-muted border-l border-border overflow-y-auto"
......@@ -98,7 +75,7 @@ function SelectionDetails({
environmentFilter,
}: {
analyzeData: AnalyzeData
modulesData: ModulesData | null
modulesData: ModulesData
selectedSourceIndex: number
moduleDepthMap: Map<number, number>
environmentFilter: 'client' | 'server'
......@@ -109,29 +86,21 @@ function SelectionDetails({
selectedSourceIndex
)
const selectedSource =
selectedSourceIndex != null
? analyzeData.source(selectedSourceIndex)
: undefined
const selectedSource = analyzeData.source(selectedSourceIndex)
const hasChildModules =
selectedSourceIndex != null &&
analyzeData.sourceChildren(selectedSourceIndex).length > 0
const childModuleCount =
hasChildModules && selectedSourceIndex != null
? analyzeData.getRecursiveModuleCount(selectedSourceIndex, filterSource)
: null
const childModuleCount = hasChildModules
? analyzeData.getRecursiveModuleCount(selectedSourceIndex, filterSource)
: null
const { size, compressedSize } = analyzeData.getRecursiveSizes(
selectedSourceIndex,
filterSource
)
const chunks =
selectedSourceIndex != null
? analyzeData.sourceChunks(selectedSourceIndex)
: []
const chunks = analyzeData.sourceChunks(selectedSourceIndex)
return (
<div className="flex-1 p-3 space-y-8 overflow-y-auto">
......@@ -139,8 +108,7 @@ function SelectionDetails({
<h2 className="text-s font-semibold mb-1 text-foreground truncate">
{selectedSource?.path || 'All Route Modules'}
</h2>
{selectedSourceIndex != null &&
analyzeData.source(selectedSourceIndex) ? (
{selectedSource ? (
<div className="text-xs">
<div>
<span>{formatBytes(compressedSize)}</span>
......@@ -173,8 +141,7 @@ function SelectionDetails({
) : null}
</div>
{selectedSourceIndex != null &&
analyzeData.source(selectedSourceIndex) &&
{selectedSource &&
(specialModuleType === SpecialModule.POLYFILL_MODULE ||
specialModuleType === SpecialModule.POLYFILL_NOMODULE) && (
<dl className="flex items-center gap-2">
......@@ -187,35 +154,31 @@ function SelectionDetails({
</dl>
)}
{selectedSourceIndex != null &&
analyzeData.source(selectedSourceIndex) &&
!hasChildModules && (
<>
{modulesData && (
<ImportChain
startFileId={selectedSourceIndex}
analyzeData={analyzeData}
modulesData={modulesData}
depthMap={moduleDepthMap}
environmentFilter={environmentFilter}
/>
)}
{chunks.length > 0 ? (
<div className="mt-2">
<p className="text-xs font-semibold text-foreground">
Output Chunks
</p>
<ul className="text-xs text-muted-foreground font-mono mt-1 space-y-1">
{chunks.map((chunk) => (
<li key={chunk} className="break-all">
{chunk}
</li>
))}
</ul>
</div>
) : null}
</>
)}
{selectedSource && !hasChildModules && (
<>
<ImportChain
startFileId={selectedSourceIndex}
analyzeData={analyzeData}
modulesData={modulesData}
depthMap={moduleDepthMap}
environmentFilter={environmentFilter}
/>
{chunks.length > 0 ? (
<div className="mt-2">
<p className="text-xs font-semibold text-foreground">
Output Chunks
</p>
<ul className="text-xs text-muted-foreground font-mono mt-1 space-y-1">
{chunks.map((chunk) => (
<li key={chunk} className="break-all">
{chunk}
</li>
))}
</ul>
</div>
) : null}
</>
)}
</div>
)
}
......@@ -238,8 +201,8 @@ export function CompareSidebar({
sourceDiff: DiffSummary<SourceDiffRow> | null
analyzeData: AnalyzeData | null
baselineAnalyzeData: AnalyzeData | null
modulesData: ModulesData | null
baselineModulesData: ModulesData | null
modulesData: ModulesData
baselineModulesData: ModulesData
moduleDepthMap: Map<number, number>
baselineModuleDepthMap: Map<number, number>
environmentFilter: 'client' | 'server'
......@@ -295,8 +258,8 @@ function CompareSidebarContent({
row: SourceDiffRow
analyzeData: AnalyzeData | null
baselineAnalyzeData: AnalyzeData | null
modulesData: ModulesData | null
baselineModulesData: ModulesData | null
modulesData: ModulesData
baselineModulesData: ModulesData
moduleDepthMap: Map<number, number>
baselineModuleDepthMap: Map<number, number>
environmentFilter: 'client' | 'server'
......
......@@ -31,3 +31,29 @@ export function TreemapSkeleton() {
</div>
)
}
export function TableSkeleton() {
return (
<div className="flex h-full w-full flex-col p-4" aria-hidden="true">
<div className="flex h-9 items-center gap-4 border-b border-border px-3">
<Skeleton className="h-3 w-1/2" />
<Skeleton className="h-3 w-24" />
<Skeleton className="h-3 w-24" />
</div>
{/* Placeholder rows fill the viewport; this is not the table's page size. */}
{Array.from({ length: 12 }, (_, index) => (
<div
key={index}
className="flex min-h-0 flex-1 items-center gap-4 border-b border-border/60 px-3"
>
<Skeleton
className="h-3"
style={{ width: `${35 + (index % 5) * 8}%` }}
/>
<Skeleton className="ml-auto h-3 w-20" />
<Skeleton className="h-3 w-20" />
</div>
))}
</div>
)
}
import useSWR from 'swr'
import useSWR, { type SWRConfiguration } from 'swr'
import { AnalyzeData, ModulesData } from './analyze-data'
import type { HistoryIndex } from './snapshot'
import { fetchStrict, jsonFetcher } from './utils'
......@@ -20,6 +20,18 @@ export function useHistoryIndex() {
})
}
export function useSuspenseJsonData<Data>(
key: string,
options: SWRConfiguration<Data> = {}
): Data {
const { data } = useSWR(key, jsonFetcher<Data>, {
...options,
suspense: true,
})
return data
}
export async function fetchAnalyzeData(url: string): Promise<AnalyzeData> {
const response = await fetchStrict(url)
return new AnalyzeData(await response.arrayBuffer())
......
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