Skip to content

refactor: convert page imports to React.lazy with Vite build-time res…#2162

Merged
jsers merged 1 commit into
mainfrom
perf-dev-server
Jul 8, 2026
Merged

refactor: convert page imports to React.lazy with Vite build-time res…#2162
jsers merged 1 commit into
mainfrom
perf-dev-server

Conversation

@jsers

@jsers jsers commented Jul 7, 2026

Copy link
Copy Markdown
Collaborator

…toration

Replace eager static imports with React.lazy() across entry.tsx, routers/index.tsx, and App.tsx to reduce initial module loading in dev.

Add two Vite plugins:

  • vite-plugin-dev-locale: loads only the active locale in serve mode to eliminate redundant language file requests
  • vite-plugin-lazy-eager: restores React.lazy back to static imports during build so prod bundles remain unaffected

Summary by CodeRabbit

  • New Features

    • Route pages now load on demand, improving initial app load and navigation performance.
    • The app now shows a loading spinner while lazy-loaded pages are being fetched.
    • Development mode can now use a single selected locale, making language behavior more consistent.
  • Bug Fixes

    • Reduced language mismatches in development by aligning runtime language selection with the active dev locale.

…toration

Replace eager static imports with React.lazy() across entry.tsx,
routers/index.tsx, and App.tsx to reduce initial module loading in dev.

Add two Vite plugins:
- vite-plugin-dev-locale: loads only the active locale in serve mode to
  eliminate redundant language file requests
- vite-plugin-lazy-eager: restores React.lazy back to static imports
  during build so prod bundles remain unaffected
@coderabbitai

coderabbitai Bot commented Jul 7, 2026

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 60b05307-ddce-4c40-ac08-388dc8ebfdf3

📥 Commits

Reviewing files that changed from the base of the PR and between 8a91604 and 76ce595.

📒 Files selected for processing (19)
  • plugins/vite-plugin-dev-locale.ts
  • plugins/vite-plugin-lazy-eager.ts
  • src/App.tsx
  • src/i18n.ts
  • src/pages/aiConfig/entry.tsx
  • src/pages/alertCurEvent/entry.tsx
  • src/pages/builtInComponents/entry.tsx
  • src/pages/contacts/entry.tsx
  • src/pages/embeddedDashboards/entry.tsx
  • src/pages/embeddedProduct/entry.tsx
  • src/pages/eventPipeline/entry.tsx
  • src/pages/hosts/entry.tsx
  • src/pages/logExplorer/entry.tsx
  • src/pages/metricsBuiltin/entry.tsx
  • src/pages/notificationChannels/entry.tsx
  • src/pages/notificationRules/entry.tsx
  • src/pages/notificationTemplates/entry.tsx
  • src/routers/index.tsx
  • vite.config.ts

📝 Walkthrough

Walkthrough

Adds two Vite plugins for single-locale dev builds and lazy-to-eager production conversion, wires them into vite.config.ts, and converts route component imports across App.tsx, routers/index.tsx, and 13 page entry files to React.lazy with Suspense fallbacks. Also updates i18n.ts for dev-mode locale override.

Changes

Route-level code splitting and locale plugin infrastructure

Layer / File(s) Summary
Dev locale and lazy-eager Vite plugins
plugins/vite-plugin-dev-locale.ts, plugins/vite-plugin-lazy-eager.ts
New plugins: devSingleLocale strips non-active locale imports during serve; lazyToEagerOnBuild rewrites React.lazy back to static imports during build for specific files.
Vite config wiring and dev locale selection
vite.config.ts, src/i18n.ts
Plugins are registered in the config; i18n.ts overrides language in DEV mode using VITE_DEV_LOCALE, falling back to zh_CN.
App.tsx and routers/index.tsx lazy conversion with Suspense
src/App.tsx, src/routers/index.tsx
Route components (TaskOutput, TaskHostOutput, SharedDetail, and dozens of router-level pages) are converted to React.lazy, with Switch wrapped in React.Suspense and a Spin fallback.
Page entry files converted to React.lazy
src/pages/aiConfig/entry.tsx, src/pages/alertCurEvent/entry.tsx, src/pages/builtInComponents/entry.tsx, src/pages/contacts/entry.tsx, src/pages/embeddedDashboards/entry.tsx, src/pages/embeddedProduct/entry.tsx, src/pages/eventPipeline/entry.tsx, src/pages/hosts/entry.tsx, src/pages/logExplorer/entry.tsx, src/pages/metricsBuiltin/entry.tsx, src/pages/notificationChannels/entry.tsx, src/pages/notificationRules/entry.tsx, src/pages/notificationTemplates/entry.tsx
Each page module switches its route component(s) from static imports to React.lazy dynamic imports while keeping route configuration unchanged.

Estimated code review effort: 3 (Moderate) | ~30 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Router as routers/index.tsx
  participant Suspense as React.Suspense
  participant Lazy as React.lazy component
  participant Browser

  Router->>Suspense: render Switch inside Suspense
  Suspense->>Lazy: trigger dynamic import
  Suspense-->>Browser: show Spin fallback
  Lazy-->>Suspense: resolve component module
  Suspense-->>Browser: render resolved route component
Loading
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch perf-dev-server

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@jsers jsers marked this pull request as ready for review July 8, 2026 02:24
Copilot AI review requested due to automatic review settings July 8, 2026 02:24
@jsers jsers merged commit 797c7af into main Jul 8, 2026
1 of 2 checks passed

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors route/page wiring to reduce Vite dev-server ESM request volume by switching many page imports to React.lazy() (so modules load on-demand in dev), while adding Vite build-time transforms to restore eager/static imports for production builds.

Changes:

  • Convert many route/page component imports (routers, App, and page entry.tsx files) to React.lazy() and wrap routing with React.Suspense fallbacks.
  • Add vite-plugin-dev-locale (serve-only) to load only the active locale in dev, and align src/i18n.ts dev language selection with VITE_DEV_LOCALE.
  • Add vite-plugin-lazy-eager (build-only) to rewrite React.lazy(() => import(...)) patterns back to static imports during vite build.

Reviewed changes

Copilot reviewed 19 out of 19 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
vite.config.ts Registers the new dev/build Vite plugins to optimize dev requests while keeping prod behavior.
src/routers/index.tsx Converts route components to React.lazy() and adds a Suspense boundary around the router switch.
src/pages/notificationTemplates/entry.tsx Lazily imports the page component from an eagerly-loaded entry module.
src/pages/notificationRules/entry.tsx Lazily imports List/Add/Edit/Detail components from an eagerly-loaded entry module.
src/pages/notificationChannels/entry.tsx Lazily imports List/Add/Edit components from an eagerly-loaded entry module.
src/pages/metricsBuiltin/entry.tsx Lazily imports the List component from an eagerly-loaded entry module.
src/pages/logExplorer/entry.tsx Lazily imports the page root to avoid pulling heavy dependencies at startup in dev.
src/pages/hosts/entry.tsx Lazily imports the List component from an eagerly-loaded entry module.
src/pages/eventPipeline/entry.tsx Lazily imports multiple route components from an eagerly-loaded entry module.
src/pages/embeddedProduct/entry.tsx Lazily imports List/Detail components from an eagerly-loaded entry module.
src/pages/embeddedDashboards/entry.tsx Lazily imports the page component from an eagerly-loaded entry module.
src/pages/contacts/entry.tsx Lazily imports the List component from an eagerly-loaded entry module.
src/pages/builtInComponents/entry.tsx Lazily imports multiple route components from an eagerly-loaded entry module.
src/pages/alertCurEvent/entry.tsx Lazily imports the List component from an eagerly-loaded entry module.
src/pages/aiConfig/entry.tsx Lazily imports multiple route components from an eagerly-loaded entry module.
src/i18n.ts Forces dev language selection to match VITE_DEV_LOCALE so dev-only single-locale loading stays consistent.
src/App.tsx Lazily loads a few top-level route components and wraps the router in Suspense.
plugins/vite-plugin-lazy-eager.ts Build-only transform to rewrite specific React.lazy(import()) patterns back to static imports.
plugins/vite-plugin-dev-locale.ts Serve-only transform to stub out non-active locale imports in locale index modules.

Comment on lines +5 to +7
* 背景:dev 下 Vite 原生 ESM「一个模块一个请求」,路由/页面若全部静态 import 会在登录页
* 就拉起数千个模块。改为 React.lazy 后 dev 只按需加载。但生产已由 Rollup 打包,不存在
* 「多请求」问题,业务希望产物保持懒加载改造前的形态(无额外按需 chunk、无路由级 Suspense)。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants