Files
Netcatty/index.tsx
T
陈大猫andClaude Opus 5.5 5098f53944 perf(renderer): cut the startup bundle by a third (#4120)
* perf(renderer): cut the startup bundle by a third

Every window parsed ~6.1 MB of JavaScript before the first render. The entry
chunk carried all five UI locales, and the whole AI SDK chunk was preloaded
even though the AI panel is lazy.

- i18n: only English stays in the entry; other dictionaries load on demand
  (plus their same-base fallback, e.g. zh-TW -> zh-CN). index.tsx awaits the
  saved language before the first render, and I18nProvider re-renders when a
  runtime switch finishes loading.
- AI runtime: aiStateSnapshots cleaned up chats through getAgentRuntime(),
  and the plugin AI/agent hosts created their runtimes on mount, which pulled
  the Catty harness and @ai-sdk providers into startup. Cleanup now goes
  through a light handle that the runtime registers (the reversible privacy
  flag and main-process privacy mapping are still cleared without a runtime),
  and both plugin hosts create their runtime on the first request, re-checking
  liveness after the async load.
- Chunking: manualChunks' object form also moved Vite's dynamic-import
  preload helper into vendor-ai (the AI SDK itself uses import()), so the
  entry imported it from there and vendor-ai (649 KB) was preloaded on every
  launch. Vendor chunks now use the function form and the helper gets its own
  1 KB chunk.
- jsqr is loaded when a QR image is imported instead of with the host editor.
- startupBundle.architecture.test.ts guards these invariants.

Startup JS (index.html): 5.29 MB entry + 649 KB vendor-ai + 138 KB radix ->
3.80 MB entry + 1 KB helper + 174 KB radix (-35%). Startup harness, 2000 hosts,
10 runs, warm p50: loadURL -> renderer entry evaluated 321 -> 207 ms,
loadURL -> renderer ready 481 -> 323 ms.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(i18n): preload locale messages before static rendering

* test(i18n): preload locales in remaining zh-CN UI tests

Non-English dictionaries now load lazily, so I18nProvider can initially render English before its effect finishes loading zh-CN. Explicitly await loadLocaleMessages before rendering these localized UI tests to fix the remaining CI failures without timing-based waits or production changes.

* chore: retrigger CI after locale preload fix

Previous tip 38108ce66 was on the branch but the PR head stayed
stuck on d49ad024e after a GitHub 500 during push. Empty commit
to refresh the PR head and re-queue checks.

* chore: retrigger CI after plugin-contract runner cancel

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 03:19:21 -08:00

280 lines
9.0 KiB
TypeScript
Executable File

import { StrictMode, Suspense, lazy } from 'react';
import ReactDOM from 'react-dom/client';
import '@fontsource/mona-sans/400.css';
import '@fontsource/mona-sans/500.css';
import '@fontsource/mona-sans/600.css';
import '@fontsource/mona-sans/700.css';
import '@fontsource/space-grotesk/400.css';
import '@fontsource/space-grotesk/500.css';
import '@fontsource/space-grotesk/600.css';
import '@fontsource/space-grotesk/700.css';
import '@fontsource/jetbrains-mono/400.css';
import '@fontsource/jetbrains-mono/500.css';
import '@fontsource/jetbrains-mono/600.css';
import App from './App';
import { AppLockGate } from './components/AppLockGate';
import { loadLocaleMessages } from './application/i18n/messages';
import { DEFAULT_UI_LOCALE, resolveSupportedLocale } from './infrastructure/config/i18n';
import { STORAGE_KEY_UI_LANGUAGE } from './infrastructure/config/storageKeys';
import { localStorageAdapter } from './infrastructure/persistence/localStorageAdapter';
// Startup timeline marks (see electron/perfTrace.cjs); visible in DevTools too.
performance.mark('netcatty:renderer:entry-evaluated');
const LazySettingsPage = lazy(() => import('./components/SettingsPage'));
const LazyTrayPanel = lazy(() => import('./components/TrayPanel'));
const LazyTerminalPopupPage = lazy(() => import('./components/TerminalPopupPage'));
const LazyEditorWindowPage = lazy(() => import('./components/editor/EditorWindowPage'));
function SettingsWindowFallback() {
return (
<div
style={{
height: '100vh',
display: 'flex',
flexDirection: 'column',
background: 'hsl(var(--background))',
color: 'hsl(var(--foreground))',
fontFamily: 'var(--font-sans)',
}}
>
<div
style={{
flexShrink: 0,
borderBottom: '1px solid hsl(var(--border))',
padding: '20px 16px 12px',
}}
>
<div
style={{
width: 96,
height: 22,
borderRadius: 6,
background: 'hsl(var(--muted) / 0.5)',
}}
/>
<div
style={{
marginTop: 8,
width: 150,
height: 14,
borderRadius: 5,
background: 'hsl(var(--muted) / 0.38)',
}}
/>
</div>
<div style={{ display: 'flex', flex: 1, minHeight: 0 }}>
<div
style={{
width: 224,
flexShrink: 0,
borderRight: '1px solid hsl(var(--border))',
padding: 12,
display: 'flex',
flexDirection: 'column',
gap: 8,
}}
>
{Array.from({ length: 7 }).map((_, index) => (
<div
key={index}
style={{
height: 36,
borderRadius: 8,
background: index === 0 ? 'hsl(var(--card))' : 'hsl(var(--muted) / 0.45)',
}}
/>
))}
</div>
<div style={{ flex: 1, padding: 20, display: 'flex', flexDirection: 'column', gap: 14 }}>
{Array.from({ length: 6 }).map((_, index) => (
<div
key={index}
style={{
height: index === 0 ? 54 : 76,
borderRadius: 12,
background: 'hsl(var(--muted) / 0.38)',
}}
/>
))}
</div>
</div>
</div>
);
}
function TerminalPopupWindowFallback() {
return (
<div
style={{
width: '100vw',
height: '100vh',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
background: '#0b1015',
color: '#d7e0ea',
}}
>
<svg width="28" height="28" viewBox="0 0 28 28" aria-label="Loading" style={{ opacity: 0.8 }}>
<circle cx="14" cy="14" r="11" fill="none" stroke="currentColor" strokeWidth="2" opacity="0.18" />
<path d="M25 14a11 11 0 0 0-11-11" fill="none" stroke="currentColor" strokeLinecap="round" strokeWidth="2">
<animateTransform
attributeName="transform"
dur="0.75s"
from="0 14 14"
repeatCount="indefinite"
to="360 14 14"
type="rotate"
/>
</path>
</svg>
</div>
);
}
const rootElement = document.getElementById('root');
if (!rootElement) {
throw new Error("Could not find root element to mount to");
}
// Simple hash-based routing for separate windows
const getRoute = () => {
const hash = window.location.hash;
if (hash === '#/settings' || hash.startsWith('#/settings')) {
return 'settings';
}
if (hash === '#/tray' || hash.startsWith('#/tray')) {
return 'tray';
}
if (hash === '#/terminal-popup' || hash.startsWith('#/terminal-popup')) {
return 'terminal-popup';
}
if (hash === '#/editor-window' || hash.startsWith('#/editor-window')) {
return 'editor-window';
}
if (hash === '#/session-window' || hash.startsWith('#/session-window')) {
return 'main';
}
return 'main';
};
const root = ReactDOM.createRoot(rootElement);
const syncTrayWindowClass = (route: string) => {
const rootEl = document.documentElement;
if (route === 'tray') {
rootEl.classList.add('tray-window');
document.getElementById('splash')?.remove();
} else {
rootEl.classList.remove('tray-window');
}
};
const renderApp = () => {
const route = getRoute();
const isPeerSessionWindow = window.location.hash.startsWith('#/session-window');
const isEditorWindow = route === 'editor-window';
// Peer session windows must not drive the main window's settings IPC sync
// and must not re-apply OS-level system settings effects (tray, global
// shortcuts, …) — they follow the main window through the chrome stores.
// Editors consume their own settings instance, so keep IPC/storage sync on.
const settingsOptions = isPeerSessionWindow
? { enableSettingsSync: false, enableSystemEffects: false }
: isEditorWindow ? { enableSystemEffects: false } : undefined;
syncTrayWindowClass(route);
if (route === 'settings') {
root.render(
<StrictMode>
<AppLockGate settingsOptions={settingsOptions}>
{({ settings, appLock }) => (
<Suspense fallback={<SettingsWindowFallback />}>
<LazySettingsPage settings={settings} appLock={appLock} />
</Suspense>
)}
</AppLockGate>
</StrictMode>
);
} else if (route === 'tray') {
root.render(
<StrictMode>
<AppLockGate settingsOptions={settingsOptions}>
{({ settings }) => (
<Suspense fallback={<div style={{ minHeight: 48, background: 'hsl(var(--background))' }} />}>
<LazyTrayPanel settings={settings} />
</Suspense>
)}
</AppLockGate>
</StrictMode>
);
} else if (route === 'terminal-popup') {
// forceRenderChildren: main sends terminalPopupConfig immediately after
// loadURL; the page must mount (and register onTerminalPopupConfig) without
// waiting for async app-lock init. notifyRendererReady stays false — this
// route is not on the deep-link readiness path.
root.render(
<StrictMode>
<AppLockGate
notifyRendererReady={false}
forceRenderChildren
settingsOptions={settingsOptions}
>
{({ settings, appLock }) => (
<Suspense fallback={<TerminalPopupWindowFallback />}>
{/* forceRenderChildren mounts the page so config IPC registers while
locked; defer starting the terminal until unlock (Codex P2). */}
<LazyTerminalPopupPage
settings={settings}
allowTerminalStart={appLock.initialized && !appLock.locked}
/>
</Suspense>
)}
</AppLockGate>
</StrictMode>
);
} else if (route === 'editor-window') {
root.render(
<StrictMode>
<AppLockGate
notifyRendererReady={false}
forceRenderChildren
settingsOptions={settingsOptions}
>
{({ settings, appLock }) => (
<Suspense fallback={<TerminalPopupWindowFallback />}>
<LazyEditorWindowPage
settings={settings}
allowEdit={appLock.initialized && !appLock.locked}
/>
</Suspense>
)}
</AppLockGate>
</StrictMode>
);
} else {
root.render(
<StrictMode>
<AppLockGate settingsOptions={settingsOptions}>
{({ settings, appLock }) => <App settings={settings} appLock={appLock} />}
</AppLockGate>
</StrictMode>
);
}
};
// Load the saved UI language before the first render (only English ships in
// the startup chunk), so no window flashes English first. Render regardless
// if the dictionary fails to load; I18nProvider retries and falls back to en.
void loadLocaleMessages(
resolveSupportedLocale(localStorageAdapter.readString(STORAGE_KEY_UI_LANGUAGE) || DEFAULT_UI_LOCALE),
)
.catch(() => {})
.finally(() => {
renderApp();
// Listen for hash changes
window.addEventListener('hashchange', renderApp);
});