Community JavaScript Snippet

Route-Level Code Splitting With React.lazy

Our initial bundle was 2.1MB. Splitting routes via React.lazy plus Suspense dropped it to 340KB on first paint. Three accordions on how I wire it.

Route-Level Code Splitting With React.lazy

Our initial bundle was 2.1MB. Splitting routes via React.lazy plus Suspense dropped it to 340KB on first paint. Three accordions on how I wire it.

JavaScript
Frontend
3 snippets
react
performance-optimization
design-patterns
meinakamura

By @meinakamura

January 24, 2026

·

Updated May 18, 2026

835 views

4

4.5 (10)

The shape is what every router-driven split eventually settles on: const Dashboard = React.lazy(() => import('./Dashboard')) and a <Suspense fallback={<Spinner />}> boundary somewhere above the route. The dynamic import() is what tells webpack or Vite to split at that point, so ./Dashboard.js and everything it transitively imports become a separate chunk that the browser only fetches when the user navigates to that route. The default-export requirement is the most-asked gotcha: lazy expects mod.default, so a named export needs a wrapper module that re-exports it as default. The harness above is a sketch of what React does; the production version handles concurrent renders, error boundaries, and cache invalidation, but the call site is the same.