Skip to content

feat: add route-level code splitting with React.lazy and Suspense (Closes #469) - #597

Open
waterWang wants to merge 1 commit into
Protocol-Guild:mainfrom
waterWang:feat/469-route-level-code-splitting
Open

feat: add route-level code splitting with React.lazy and Suspense (Closes #469)#597
waterWang wants to merge 1 commit into
Protocol-Guild:mainfrom
waterWang:feat/469-route-level-code-splitting

Conversation

@waterWang

Copy link
Copy Markdown

Summary

Converts all 20 page components in App.tsx from eager imports to React.lazy() dynamic imports, enabling route-level code splitting. Each page chunk loads on demand, reducing the initial bundle size.

Changes

Code Splitting

  • All 20 page imports replaced with React.lazy(() => import('./pages/...'))
  • Layout/utility components (EmployerLayout, ErrorBoundary, ErrorFallback) remain as regular imports since they are not route pages
  • Suspense wraps the entire <Routes> block with a centered loading spinner (Loader2 from lucide-react)

Loading Fallback

  • PageLoader component shows a centered spinner with "Loading…" text
  • Uses Loader2 with animate-spin from the existing lucide-react dependency

Error Handling

  • Each route already has its own ErrorBoundary wrapper — chunk load failures are caught by the existing error boundary hierarchy
  • The outer Suspense fallback only shows during initial chunk loading; runtime errors are handled by the per-route ErrorBoundary

Bundle Impact

  • Vite automatically splits each lazy import into a separate chunk
  • The initial HTML/JS payload no longer includes all 20 page components
  • Verified with tsc --noEmit — zero type errors

Verification

  • tsc --noEmit passes cleanly
  • lucide-react is already a project dependency (Loader2 used in CrossAssetPayment.tsx)
  • Pattern matches Vite's recommended code-splitting approach

Closes #469

- Convert all 20 page imports to React.lazy() dynamic imports
- Add Suspense wrapper with loading spinner (Loader2 from lucide-react)
- Keep layout/utility components (EmployerLayout, ErrorBoundary,
  ErrorFallback) as regular imports since they are not route pages
- Each page chunk loads on demand, reducing initial bundle size

Closes Protocol-Guild#469
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.

Add route-level code splitting with React.lazy and Suspense

1 participant