Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

feat([codex5-bundle-lazy-routes]): lazy-load dashboard widgets and add bundle visualizer

[._.]/ Adam Eivyeaab785778f0
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.md
index ed07de7a31..a9295c4466 100644
--- a/.changelog/NEXT.md
+++ b/.changelog/NEXT.md
@@ -6,6 +6,7 @@
- **[cover-prose-input-idea-input-in-canonusage-corpus] Universe canon cross-reference test suite now pins every stage the per-issue search reads** — a regression that silently drops a stage from the search now fails the test suite immediately.
- **[extract-shared-requiretoolkit-helper] Internal: consolidated three duplicated AI toolkit accessors into one shared module.** No behavior change; cuts ~30 lines and makes future toolkit-state changes one-touch.
+- **[codex5-bundle-lazy-routes] Dashboard widgets load on demand.** A dashboard layout now only downloads the widgets it actually uses — the first paint of a slimmed-down layout no longer drags in every widget in the registry, and slow widgets render their own placeholder while loading instead of blocking sibling cells. Also adds an `npm run build:analyze` script for inspecting bundle composition during development.
## Fixed
diff --git a/PLAN.md b/PLAN.md
index 2e6bb8753e..8907a05ef5 100644
--- a/PLAN.md
+++ b/PLAN.md
@@ -29,7 +29,6 @@ The 2026-04-25 CODEX 5.5 review's concrete bugs (Brain Feeds nav, Time Capsule n
**Engineering / performance**
-- [ ] [codex5-bundle-lazy-routes] **[P1][PERF]** Route-level + dashboard-widget lazy loading. Review measured `dist/assets/index-*.js` at ~2.95 MB minified / ~736 KB gzip. `App.jsx` statically imports several heavy pages (`Brain`, `DigitalTwin`, `MeatSpace`, `Messages`, `Dashboard`) and the widget registry pulls all widgets into the initial graph. Lazy-load via `React.lazy` + `Suspense`; convert `widgetRegistry.jsx` entries to lazy components so widgets only download when their layout includes them. Add a bundle-visualization step (`rollup-plugin-visualizer`) and track regressions.
- [ ] [codex5-polling-consolidation] **[P2][PERF]** Standardize polling on `useAutoRefetch` + sockets + visibility pause. Brain, Digital Twin, CoS, Links, Processes, Browser, CyberCity, and several widgets still spin local `setInterval`s. Pick a single hook (`client/src/hooks/useAutoRefetch.js` if it exists, otherwise extract one), add `document.visibilityState === 'hidden'` short-circuit, and migrate domains that already have Socket.IO event streams to event-driven refetch.
- [ ] [codex5-client-error-reporter] **[P2][OBSERVABILITY]** Funnel client errors into Review Hub. `client/src/main.jsx`'s `window.onerror` + `unhandledrejection` listeners only `console.log` today. Add `POST /api/client-errors` with redaction (drop tokens / long payloads) + rate-limit (~1/sec, drop duplicates by message+stack hash). Surface grouped client errors in the Review Hub feed; optionally raise a CoS task at a configurable threshold.
diff --git a/client/package.json b/client/package.json
index d1bb976d0e..fec3705a36 100644
--- a/client/package.json
+++ b/client/package.json
@@ -6,6 +6,7 @@
"scripts": {
"dev": "vite --host 0.0.0.0 --port 5554",
"build": "vite build",
+ "build:analyze": "ANALYZE=true vite build",
"lint": "eslint src --ext .js,.jsx",
"preview": "vite preview",
"test": "vitest run",
@@ -39,6 +40,7 @@
"eslint-plugin-react": "7.37.5",
"eslint-plugin-react-hooks": "5.2.0",
"jsdom": "25.0.1",
+ "rollup-plugin-visualizer": "^6.0.11",
"tailwindcss": "4.2.1",
"vite": "7.3.2",
"vitest": "3.2.4"
diff --git a/client/src/App.jsx b/client/src/App.jsx
index c6551706a8..582e65fef3 100644
--- a/client/src/App.jsx
+++ b/client/src/App.jsx
@@ -1,4 +1,4 @@
-import { Suspense, lazy, useEffect } from 'react';
+import { Suspense, useEffect } from 'react';
import { Routes, Route, Navigate, useLocation } from 'react-router-dom';
import Layout from './components/Layout';
import { getSettings, updateSettings } from './services/api';
@@ -6,18 +6,7 @@ import BrailleSpinner from './components/BrailleSpinner';
import Dashboard from './pages/Dashboard';
import Apps from './pages/Apps';
import Ambient from './pages/Ambient';
-import { isStaleChunkError, reloadOnceForStaleChunk } from './utils/staleChunkReload';
-
-// Auto-reload on stale chunk errors (e.g., after a rebuild changes chunk hashes).
-// Detection covers Chrome / Firefox / Safari variants — see staleChunkReload.js.
-const lazyWithReload = (importFn) => lazy(() =>
- importFn().catch(err => {
- if (isStaleChunkError(err) && reloadOnceForStaleChunk()) {
- return new Promise(() => {}); // hang until reload completes
- }
- throw err;
- })
-);
+import { lazyWithReload } from './utils/lazyWithReload';
// Lazy load heavier pages for code splitting
// DevTools pages are large (~2300 lines total) so lazy load them
diff --git a/client/src/components/dashboard/WidgetSkeleton.jsx b/client/src/components/dashboard/WidgetSkeleton.jsx
new file mode 100644
index 0000000000..41b1687b0f
--- /dev/null
+++ b/client/src/components/dashboard/WidgetSkeleton.jsx
@@ -0,0 +1,13 @@
+// Placeholder rendered while a lazy-loaded dashboard widget is in flight.
+// Fills the grid cell so the dashboard layout doesn't reflow during fetch.
+export default function WidgetSkeleton({ label }) {
+ return (
+ <div
+ role="status"
+ aria-label={label ? `Loading ${label}` : 'Loading widget'}
+ className="h-full w-full rounded-lg border border-port-border bg-port-card animate-pulse flex items-center justify-center text-xs text-gray-500"
+ >
+ {label ?? 'Loading…'}
+ </div>
+ );
+}
diff --git a/client/src/components/dashboard/widgetRegistry.jsx b/client/src/components/dashboard/widgetRegistry.jsx
index 279848893d..326e4e437c 100644
--- a/client/src/components/dashboard/widgetRegistry.jsx
+++ b/client/src/components/dashboard/widgetRegistry.jsx
@@ -1,19 +1,24 @@
-import BackupWidget from '../BackupWidget';
-import NetworkExposureWidget from '../NetworkExposureWidget';
-import SystemHealthWidget from '../SystemHealthWidget';
-import CosDashboardWidget from '../CosDashboardWidget';
-import GoalProgressWidget from '../GoalProgressWidget';
-import UpcomingTasksWidget from '../UpcomingTasksWidget';
-import DecisionLogWidget from '../DecisionLogWidget';
-import DeathClockWidget from '../DeathClockWidget';
-import ProactiveAlertsWidget from '../ProactiveAlertsWidget';
-import QuickBrainCapture from '../QuickBrainCapture';
-import QuickTaskWidget from '../QuickTaskWidget';
-import ReviewHubCard from '../ReviewHubCard';
-import AppsGridWidget from './builtins/AppsGridWidget';
-import QuickStatsWidget from './builtins/QuickStatsWidget';
-import ActivityStreakWidget from './builtins/ActivityStreakWidget';
-import HourlyActivityWidget from './builtins/HourlyActivityWidget';
+import { lazyWithReload } from '../../utils/lazyWithReload';
+
+// Widgets are lazy-loaded so a layout only downloads the widgets it actually
+// uses. The Dashboard render path wraps each <Component> in <Suspense> with a
+// per-cell skeleton so a slow widget can't stall sibling cells in the grid.
+const BackupWidget = lazyWithReload(() => import('../BackupWidget'));
+const NetworkExposureWidget = lazyWithReload(() => import('../NetworkExposureWidget'));
+const SystemHealthWidget = lazyWithReload(() => import('../SystemHealthWidget'));
+const CosDashboardWidget = lazyWithReload(() => import('../CosDashboardWidget'));
+const GoalProgressWidget = lazyWithReload(() => import('../GoalProgressWidget'));
+const UpcomingTasksWidget = lazyWithReload(() => import('../UpcomingTasksWidget'));
+const DecisionLogWidget = lazyWithReload(() => import('../DecisionLogWidget'));
+const DeathClockWidget = lazyWithReload(() => import('../DeathClockWidget'));
+const ProactiveAlertsWidget = lazyWithReload(() => import('../ProactiveAlertsWidget'));
+const QuickBrainCapture = lazyWithReload(() => import('../QuickBrainCapture'));
+const QuickTaskWidget = lazyWithReload(() => import('../QuickTaskWidget'));
+const ReviewHubCard = lazyWithReload(() => import('../ReviewHubCard'));
+const AppsGridWidget = lazyWithReload(() => import('./builtins/AppsGridWidget'));
+const QuickStatsWidget = lazyWithReload(() => import('./builtins/QuickStatsWidget'));
+const ActivityStreakWidget = lazyWithReload(() => import('./builtins/ActivityStreakWidget'));
+const HourlyActivityWidget = lazyWithReload(() => import('./builtins/HourlyActivityWidget'));
// Each entry: { id, label, Component, width, defaultH?, gate?, module? }.
// `gate(state) => bool` skips the widget when it has nothing useful to show.
diff --git a/client/src/pages/Dashboard.jsx b/client/src/pages/Dashboard.jsx
index 489eb5e19d..87df8c6438 100644
--- a/client/src/pages/Dashboard.jsx
+++ b/client/src/pages/Dashboard.jsx
@@ -1,10 +1,11 @@
-import { useState, useEffect, useCallback, useMemo } from 'react';
+import { useState, useEffect, useCallback, useMemo, Suspense } from 'react';
import { Link } from 'react-router-dom';
import BrailleSpinner from '../components/BrailleSpinner';
import LayoutPicker from '../components/dashboard/LayoutPicker';
import LayoutEditor from '../components/dashboard/LayoutEditor';
import DashboardGrid, { reconcileGrid, synthesizeGrid } from '../components/dashboard/DashboardGrid.jsx';
import { WIDGETS_BY_ID, FALLBACK_LAYOUT } from '../components/dashboard/widgetRegistry.jsx';
+import WidgetSkeleton from '../components/dashboard/WidgetSkeleton';
import { SchematicLabel } from '../components/micrographics';
import { DASHBOARD_LAYOUT_CHANGED } from '../constants/events.js';
import { Monitor, Move, Save, X } from 'lucide-react';
@@ -335,7 +336,12 @@ export default function Dashboard() {
renderItem={(item) => {
const meta = WIDGETS_BY_ID[item.id];
if (!meta) return null;
- const widget = <meta.Component dashboardState={dashboardState} />;
+ // Per-cell Suspense so a slow widget can't block sibling cells.
+ const widget = (
+ <Suspense fallback={<WidgetSkeleton label={meta.label} />}>
+ <meta.Component dashboardState={dashboardState} />
+ </Suspense>
+ );
if (!meta.module) return widget;
// Tab sits inside the wrapper (DashboardGrid clips with
// overflow-hidden); sm:pt-4 reserves a header zone so
diff --git a/client/src/utils/lazyWithReload.js b/client/src/utils/lazyWithReload.js
new file mode 100644
index 0000000000..48cb53553f
--- /dev/null
+++ b/client/src/utils/lazyWithReload.js
@@ -0,0 +1,15 @@
+import { lazy } from 'react';
+import { isStaleChunkError, reloadOnceForStaleChunk } from './staleChunkReload';
+
+// React.lazy() wrapper that recovers from stale-chunk errors after a rebuild
+// changes Vite chunk hashes while a tab is still open. When such an error
+// fires, reload the page once to pick up the new bundle; everything else
+// re-throws so error boundaries can see it.
+export const lazyWithReload = (importFn) => lazy(() =>
+ importFn().catch(err => {
+ if (isStaleChunkError(err) && reloadOnceForStaleChunk()) {
+ return new Promise(() => {});
+ }
+ throw err;
+ })
+);
diff --git a/client/vite.config.js b/client/vite.config.js
index 145a15ef5f..ad6b122127 100644
--- a/client/vite.config.js
+++ b/client/vite.config.js
@@ -1,8 +1,11 @@
import { defineConfig, loadEnv } from 'vite';
import react from '@vitejs/plugin-react';
+import { visualizer } from 'rollup-plugin-visualizer';
import { readFileSync, existsSync } from 'fs';
import { resolve } from 'path';
+const ANALYZE_BUNDLE = process.env.ANALYZE === 'true';
+
const rootPkg = JSON.parse(readFileSync(resolve(__dirname, '../package.json'), 'utf-8'));
// Dev proxy target: probe for the self-signed/LE cert under data/certs/. If the
@@ -21,7 +24,15 @@ export default defineConfig(({ mode }) => {
define: {
__APP_VERSION__: JSON.stringify(rootPkg.version)
},
- plugins: [react()],
+ plugins: [
+ react(),
+ ANALYZE_BUNDLE && visualizer({
+ filename: 'dist/bundle-report.html',
+ gzipSize: true,
+ brotliSize: true,
+ template: 'treemap',
+ }),
+ ].filter(Boolean),
server: {
host: '0.0.0.0',
port: 5554,