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

Fix bundle analyzer route selector (#98702)

Fix route selector overflow for long route names and constrain the
popover to the viewport.

Deduplicate route names when generating analyzer data and before
rendering the picker so parallel routes and existing analyzer corpora
cannot produce duplicate command items.

<!-- 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 d71d13d3
...@@ -88,11 +88,13 @@ export function RouteTypeahead({ ...@@ -88,11 +88,13 @@ export function RouteTypeahead({
// the most-changed route bubbles to the top — matching the rest of the // the most-changed route bubbles to the top — matching the rest of the
// compare UI. Without a diff, fall back to the natural routes.json order. // compare UI. Without a diff, fall back to the natural routes.json order.
const orderedItems: RouteItem[] = routeDiff const orderedItems: RouteItem[] = routeDiff
? sortByImpact(routeDiff.rows, useCompressed).map((row) => ({ ? uniqueRouteItems(
name: row.key, sortByImpact(routeDiff.rows, useCompressed).map((row) => ({
row, name: row.key,
})) row,
: routes.map((name) => ({ name, row: null })) }))
)
: uniqueRouteItems(routes.map((name) => ({ name, row: null })))
// Find the currently selected route's diff row, used to render a delta // Find the currently selected route's diff row, used to render a delta
// badge in the trigger button. // badge in the trigger button.
...@@ -104,35 +106,45 @@ export function RouteTypeahead({ ...@@ -104,35 +106,45 @@ export function RouteTypeahead({
const ctaText = selectedRoute ?? 'Select route...' const ctaText = selectedRoute ?? 'Select route...'
return ( return (
<div className="flex items-center gap-2 min-w-64 max-w-full"> <div className="flex min-w-0 items-center gap-2 sm:min-w-64">
<Popover open={open} onOpenChange={setOpen}> <Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild> <PopoverTrigger asChild>
<Button <Button
variant="outline" variant="outline"
role="combobox" role="combobox"
aria-expanded={open} aria-expanded={open}
className="flex-grow-1 w-full justify-between font-mono text-sm" aria-label={
selectedRoute
? `Select route. Current route: ${selectedRoute}`
: 'Select route'
}
className="w-full min-w-0 justify-between font-mono text-sm"
> >
<div className="flex items-center min-w-0"> <div className="flex min-w-0 flex-1 items-center">
<Route className="inline mr-2 shrink-0" /> <Route className="inline mr-2 shrink-0" />
<span className="truncate">{ctaText}</span> <span className="min-w-0 flex-1 truncate" title={ctaText}>
{truncateMiddle(ctaText, 32)}
</span>
{selectedRow ? ( {selectedRow ? (
<DeltaBadge row={selectedRow} useCompressed={useCompressed} /> <DeltaBadge row={selectedRow} useCompressed={useCompressed} />
) : null} ) : null}
</div> </div>
<div className="flex items-center gap-2"> <div className="ml-2 flex shrink-0 items-center gap-2">
{shortcutLabel && <Kbd>{shortcutLabel}</Kbd>} {shortcutLabel && <Kbd>{shortcutLabel}</Kbd>}
<ChevronsUpDown className="h-4 w-4 shrink-0 opacity-50" /> <ChevronsUpDown className="h-4 w-4 shrink-0 opacity-50" />
</div> </div>
</Button> </Button>
</PopoverTrigger> </PopoverTrigger>
<PopoverContent className="w-96 p-0"> <PopoverContent
<Command> align="start"
className="w-[40rem] max-w-[calc(100vw-1rem)] overflow-hidden p-0"
>
<Command className="min-w-0">
<CommandInput placeholder="Search routes..." className="h-9" /> <CommandInput placeholder="Search routes..." className="h-9" />
<CommandList> <CommandList className="min-w-0">
<CommandEmpty>No route found.</CommandEmpty> <CommandEmpty>No route found.</CommandEmpty>
<CommandGroup> <CommandGroup className="min-w-0 [&_[cmdk-group-items]]:min-w-0">
{orderedItems.map(({ name, row }) => ( {orderedItems.map(({ name, row }) => (
<CommandItem <CommandItem
key={name} key={name}
...@@ -141,7 +153,7 @@ export function RouteTypeahead({ ...@@ -141,7 +153,7 @@ export function RouteTypeahead({
onRouteSelected(name) onRouteSelected(name)
setOpen(false) setOpen(false)
}} }}
className="font-mono" className="w-full min-w-0 overflow-hidden font-mono"
> >
<Check <Check
className={cn( className={cn(
...@@ -149,7 +161,14 @@ export function RouteTypeahead({ ...@@ -149,7 +161,14 @@ export function RouteTypeahead({
selectedRoute === name ? 'opacity-100' : 'opacity-0' selectedRoute === name ? 'opacity-100' : 'opacity-0'
)} )}
/> />
<span className="truncate">{name}</span> <span className="sr-only">{name}</span>
<span
aria-hidden="true"
className="min-w-0 flex-1 truncate"
title={name}
>
{truncateMiddle(name, 64)}
</span>
{row ? ( {row ? (
<DeltaBadge <DeltaBadge
row={row} row={row}
...@@ -173,6 +192,23 @@ interface RouteItem { ...@@ -173,6 +192,23 @@ interface RouteItem {
row: DiffRow | null row: DiffRow | null
} }
function truncateMiddle(value: string, maxLength: number): string {
if (value.length <= maxLength) return value
const startLength = Math.ceil((maxLength - 1) / 2)
const endLength = Math.floor((maxLength - 1) / 2)
return `${value.slice(0, startLength)}…${value.slice(-endLength)}`
}
function uniqueRouteItems(items: RouteItem[]): RouteItem[] {
const names = new Set<string>()
return items.filter(({ name }) => {
if (names.has(name)) return false
names.add(name)
return true
})
}
/** /**
* Compact, color-coded badge showing a route's size delta. Hidden when the * Compact, color-coded badge showing a route's size delta. Hidden when the
* row has no meaningful change. * row has no meaningful change.
......
...@@ -102,7 +102,7 @@ export function TopBar({ ...@@ -102,7 +102,7 @@ export function TopBar({
const isCompareMode = baselineSnapshot != null const isCompareMode = baselineSnapshot != null
return ( return (
<div className="flex-none px-4 py-2 border-b border-border flex items-center gap-3"> <div className="flex-none px-4 py-2 border-b border-border flex items-center gap-3">
<div className="flex-1 flex"> <div className="flex min-w-0 flex-1">
<RouteTypeahead <RouteTypeahead
selectedRoute={selectedRoute} selectedRoute={selectedRoute}
onRouteSelected={(route) => { onRouteSelected={(route) => {
......
...@@ -199,9 +199,13 @@ async function collectRoutesForAnalyze( ...@@ -199,9 +199,13 @@ async function collectRoutesForAnalyze(
isAppPPREnabled, isAppPPREnabled,
}) })
return routesManifest.dynamicRoutes return Array.from(
.map((r) => r.page) new Set(
.concat(routesManifest.staticRoutes.map((r) => r.page)) routesManifest.dynamicRoutes
.map((r) => r.page)
.concat(routesManifest.staticRoutes.map((r) => r.page))
)
)
} }
function startServer(dir: string, port: number): Promise<void> { function startServer(dir: string, port: number): Promise<void> {
......
...@@ -4763,9 +4763,13 @@ export default async function build( ...@@ -4763,9 +4763,13 @@ export default async function build(
}) })
// Write an index of routes for the route picker // Write an index of routes for the route picker
const routes = routesManifest.dynamicRoutes const routes = Array.from(
.map((r) => r.page) new Set(
.concat(routesManifest.staticRoutes.map((r) => r.page)) routesManifest.dynamicRoutes
.map((r) => r.page)
.concat(routesManifest.staticRoutes.map((r) => r.page))
)
)
await writeFile( await writeFile(
path.join(analyzeDir, 'data/routes.json'), path.join(analyzeDir, 'data/routes.json'),
JSON.stringify(routes, null, 2) JSON.stringify(routes, null, 2)
......
export default function SlotPage() {
return <div>Slot</div>
}
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