From ecb8064ca9220900a2d42032b58780df7d813704 Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Tue, 19 May 2026 11:48:41 +0800 Subject: [PATCH] perf(webui): code-split routes; cut initial JS from 606 kB to 284 kB MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `vite build` was emitting a single ~606 kB chunk for the whole SPA (Vite even printed its >500 kB warning). Every route — overview, config (with its full edit-dialog form surface), logs, settings — was eagerly imported in `router/index.tsx`, so users paid the full parse + execute cost before the login redirect even resolved. That's the most plausible cause of the "loading node config is somewhat slow" report. Switch each route to `lazyRouteComponent(() => import('…'), '')` so the page chunk is fetched on demand, and wrap `` in `AppLayout` with a `` boundary backed by a small skeleton fallback. `defaultPreload: 'intent'` is already set on the router, so hovering a sidebar link warms the next chunk before the click — navigation still feels instant after the first idle moment. Build now emits: index.js 606 kB → 284 kB (-53%) overview-page 15 kB config-page 25 kB logs-page 5.6 kB settings-page 5.8 kB button (shared primitives) 164 kB + smaller per-component chunks Vite shook out And the >500 kB warning is gone. Bonus: replace the per-render `list.filter(...).map(...).includes(item.name)` O(n²) duplicate-name check in `NetworkTabView` with a one-shot `Map` — matters every time the user types in the edit dialog and the tab underneath re-renders. --- .../src/components/pages/config-page.tsx | 17 ++++++++-- packages/webui/src/router/app-layout.tsx | 27 +++++++++++++-- packages/webui/src/router/index.tsx | 33 ++++++++++++++----- 3 files changed, 65 insertions(+), 12 deletions(-) diff --git a/packages/webui/src/components/pages/config-page.tsx b/packages/webui/src/components/pages/config-page.tsx index a9954fac..fa7594f4 100644 --- a/packages/webui/src/components/pages/config-page.tsx +++ b/packages/webui/src/components/pages/config-page.tsx @@ -353,6 +353,20 @@ function NetworkTabView({ // widening cast at the call site rather than ladder-of-ifs per tab. const summarize = tab.summarize as (it: typeof list[number]) => string; + // Tally name occurrences once instead of doing `list.filter(...).map(...).includes(...)` + // inside `.map()` (O(n²) per render). With this lookup, the duplicate + // check is O(1) per row — matters once the list grows past a few entries + // or when typing renames a node and the tab re-renders on every keystroke. + const nameCounts = useMemo(() => { + const counts = new Map(); + for (const item of list) { + const name = item.name; + if (!name) continue; + counts.set(name, (counts.get(name) ?? 0) + 1); + } + return counts; + }, [list]); + if (list.length === 0) { return (
@@ -369,8 +383,7 @@ function NetworkTabView({

{tab.description}

{list.map((item, idx) => { - const others = list.filter((_, i) => i !== idx).map((n) => n.name); - const duplicate = !!item.name && others.includes(item.name); + const duplicate = !!item.name && (nameCounts.get(item.name) ?? 0) > 1; return ( - + {/* Routes use `lazyRouteComponent` (router/index.tsx) for + code-splitting, which suspends until the chunk is fetched. + The chrome (sidebar / top bar) stays mounted across this + boundary so only the page surface flashes a skeleton. */} + }> + + ); } + +function PageFallback() { + // Generic page placeholder. Kept intentionally low-detail (just a + // header + a couple of card-shaped blocks) so it works for every + // route — overview's stat grid, config's tabbed editor, logs' list, + // and the settings page all converge on roughly this skeleton on + // their own loading states. + return ( +
+ + + + +
+ ); +} diff --git a/packages/webui/src/router/index.tsx b/packages/webui/src/router/index.tsx index 21e828b0..791dc490 100644 --- a/packages/webui/src/router/index.tsx +++ b/packages/webui/src/router/index.tsx @@ -2,14 +2,19 @@ import { createRootRoute, createRoute, createRouter, + lazyRouteComponent, Outlet, } from '@tanstack/react-router'; -import { OverviewPage } from '@/components/pages/overview-page'; -import { ConfigPage } from '@/components/pages/config-page'; -import { LogsPage } from '@/components/pages/logs-page'; -import { SettingsPage } from '@/components/pages/settings-page'; import { AppLayout } from './app-layout'; +// Page components are loaded on demand so the initial paint only ships +// the auth surface + layout shell. With `defaultPreload: 'intent'` set +// below, the router warms the next chunk on hover/focus, so navigation +// still feels instant after the first idle moment. Previously every +// page (overview / config / logs / settings) lived in the single +// ~600 kB bundle that Vite explicitly warned about — config-page edit +// dialog alone pulls in a large form surface that most users never +// touch on first visit. const rootRoute = createRootRoute({ component: () => , }); @@ -23,25 +28,37 @@ const appLayoutRoute = createRoute({ const overviewRoute = createRoute({ path: '/', getParentRoute: () => appLayoutRoute, - component: OverviewPage, + component: lazyRouteComponent( + () => import('@/components/pages/overview-page'), + 'OverviewPage', + ), }); const configRoute = createRoute({ path: '/config', getParentRoute: () => appLayoutRoute, - component: ConfigPage, + component: lazyRouteComponent( + () => import('@/components/pages/config-page'), + 'ConfigPage', + ), }); const logsRoute = createRoute({ path: '/logs', getParentRoute: () => appLayoutRoute, - component: LogsPage, + component: lazyRouteComponent( + () => import('@/components/pages/logs-page'), + 'LogsPage', + ), }); const settingsRoute = createRoute({ path: '/settings', getParentRoute: () => appLayoutRoute, - component: SettingsPage, + component: lazyRouteComponent( + () => import('@/components/pages/settings-page'), + 'SettingsPage', + ), }); const routeTree = rootRoute.addChildren([