🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)
🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)

🔧 Programmierung 🕛 kürzlich 9 Min Lesezeit
0

React.lazy() Route Code Splitting Explained

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

Related:









What React.lazy() does to the bundle



React.lazy() takes a function that returns a dynamic import(). The key word is dynamic. A static import at the top of a file is analyzed at build time and the imported module is included in the same output chunk. A dynamic import is a split point: the bundler creates a separate output chunk containing the dynamically imported module and all of its unique dependencies.




CODE
// Static import: Dashboard ends up in the same chunk as App

// Dynamic import with React.lazy(): Dashboard becomes a separate chunk
const Dashboard = lazy(() => import('./pages/Dashboard'));






When you switch from a static import to React.lazy(), your bundler (Vite, Webpack, or otherwise) creates a new file in the build output. Instead of one bundle, you get the main bundle plus a chunk file specifically for Dashboard and everything it imports that is not already in the main bundle.



The main bundle shrinks by the size of Dashboard and its unique dependencies. The Dashboard chunk is downloaded on demand when the user navigates to the dashboard route.




CODE

const Home = lazy(() => import('./pages/Home'));
const Dashboard = lazy(() => import('./pages/Dashboard'));
const Settings = lazy(() => import('./pages/Settings'));
const AdminPanel = lazy(() => import('./pages/AdminPanel'));

function App() {
return (
}>

</Suspense>
);
}






In this setup, a user who lands on / downloads the main bundle plus the Home chunk. They do not download anything for Dashboard, Settings, or AdminPanel until they navigate to those routes.









How Suspense fits in



React.lazy() by itself will throw an error if you try to render the component before its chunk has loaded. Suspense catches that error and shows a fallback UI while the chunk downloads.



The mechanics: when React tries to render a lazy component and the chunk is not yet loaded, the component throws a Promise. Suspense catches the thrown Promise and renders the fallback. When the Promise resolves (the chunk has downloaded), Suspense re-renders the tree with the actual component.



This is the same Suspense boundary you would use for data fetching. The behavior is identical: throw a Promise to signal "not ready," catch it at the nearest Suspense boundary, show fallback while waiting, render real content when ready.




CODE
// Suspense can wrap individual routes or the entire app
// Wrapping the whole app is simpler; wrapping per-route allows per-route fallbacks

// Per-route fallbacks (more control):
function App() {
return (

}
/>
}>

</Suspense>
}
/>
</Routes>
);
}






For most applications, a single Suspense boundary around all routes is simpler and sufficient. The fallback can be a generic page loader or even null if you prefer a blank transition. The key constraint is that the Suspense boundary must be an ancestor of the lazy component in the React tree.









Verifying the split actually happened



The most common mistake with code splitting is thinking you have done it when you have not. Before assuming a React.lazy() conversion worked, verify it.



Method 1: Check the build output. Run npm run build and look at the output files. A successful route split produces separate chunk files:




CODE
dist/assets/index-a1b2c3.js       (main bundle, ~80KB)
dist/assets/Dashboard-d4e5f6.js (dashboard chunk, ~45KB)
dist/assets/Settings-g7h8i9.js (settings chunk, ~22KB)
dist/assets/AdminPanel-j0k1l2.js (admin chunk, ~38KB)






If you see only one JavaScript file or if all your route components appear in index.js, the split did not happen.



Method 2: Use bundle analysis. With Vite, add the visualizer plugin:




CODE
// vite.config.ts

plugins: [
visualizer({ filename: 'bundle-stats.html', gzipSize: true })
]
};






After building, open bundle-stats.html. If the split worked, you will see your route components in separate rectangles outside the main bundle rectangle. If they are inside the main bundle, something went wrong.



Method 3: Check the Network tab. Open Chrome DevTools, go to the Network tab, filter by JS, and navigate between routes. When you navigate to a route for the first time, you should see a new JS file appear in the network requests. If no new files appear, the code was included in the initial bundle.









Why the split silently fails



Static imports elsewhere in the file. If you import a component from a file that is also imported statically somewhere in the main bundle, the component will be included in the main bundle regardless of your lazy() call. The bundler deduplicates modules, so a module that is reachable through a static import path will not be split out.




CODE
// This file statically imports Dashboard, so Dashboard is in the main bundle

// This lazy() call does nothing useful because Dashboard is already included
const DashboardLazy = lazy(() => import('./pages/Dashboard'));






Check that the component you are splitting is not imported anywhere else in the main bundle's dependency chain.



Eager imports in the entry point. The entry point (usually main.tsx or App.tsx) must not have static imports for the components you want to split.




CODE
// main.tsx: these static imports pull everything into the initial bundle

// All of these are now in the initial bundle regardless of lazy() usage below






If your entry file imports these components, remove those imports and rely entirely on React.lazy() for the route components.



Shared dependencies. If Dashboard and Settings both import a large shared library, that library goes into the main chunk (or a separate vendor chunk) rather than being duplicated in each route chunk. This is correct behavior and not a failure. The route chunks will be smaller than you expect because shared code is extracted automatically.









Named chunks for production debugging



By default, bundlers generate hash-based chunk names like chunk-d4e5f6.js. These are stable for caching but unreadable in production error logs.



Add a magic comment to name chunks:




CODE
const Dashboard = lazy(() =>
import(
/* webpackChunkName: "dashboard" */
'./pages/Dashboard'
)
);

// In Vite, use vitePreload (Vite handles naming automatically based on file names in most cases)
// But you can also add rollupOptions in vite.config.ts to control chunk naming






With named chunks, your build output becomes:




CODE
dist/assets/dashboard-d4e5f6.js
dist/assets/settings-g7h8i9.js
dist/assets/admin-panel-j0k1l2.js






Production stack traces and network logs become readable.









Preloading chunks on hover to eliminate waterfall



The biggest UX issue with lazy loading is the delay when a user first navigates to a route. The sequence is: user clicks link, React renders null (Suspense fallback), browser fetches chunk, chunk executes, component renders. That network fetch adds latency.



The standard fix is to preload the chunk before the user clicks, triggered on hover or focus:




CODE
// Preload helper
function preloadComponent(factory) {
const Component = lazy(factory);
// Trigger the import() immediately (while hovering)
// React.lazy caches the result, so when Suspense renders it, the chunk is ready
Component.preload = factory;
return Component;
}

const Dashboard = lazy(() => import('./pages/Dashboard'));

// In your nav link:
function NavLink({ to, children }) {
const navigate = useNavigate();

const handleMouseEnter = () => {
// Trigger the chunk fetch when user hovers the link
import('./pages/Dashboard');
};

return (
<a
href={to}
onMouseEnter={handleMouseEnter}
onFocus={handleMouseEnter}
>
{children}
</a>
);
}






When the user hovers a navigation link, the chunk download starts. By the time they click and React tries to render the route, the chunk is already in the browser's cache. The Suspense fallback either never shows or shows for under 100ms.



React Router v6 handles this through the loader pattern in the data router API. Next.js has built-in prefetching for <Link> components. For custom setups, the hover-triggered import approach above covers most cases.









The numbers in practice



Here is a realistic example from a mid-size React application before and after route splitting:






































Route Before (included in initial bundle) After (separate chunk, loaded on demand)
Homepage 1.2MB total initial JS 85KB initial bundle
Dashboard (already loaded) 245KB, loads when user navigates
Settings (already loaded) 78KB, loads when user navigates
Admin Panel (already loaded) 312KB, loads when user navigates
Reports (already loaded) 168KB, loads when user navigates


A user who only uses the homepage never downloads the Dashboard chunk. A user who does use the dashboard downloads 245KB for it specifically, rather than getting it bundled with everything else. The browser can also cache each chunk separately, so after a first visit to the dashboard, subsequent visits serve it from cache regardless of whether the main bundle changed.



The improvement in Time to Interactive for first-time visitors is proportional to how much of the pre-split bundle was route-specific code. In applications with many routes and large per-route dependencies, the improvement can be dramatic. In small applications with 3 routes and shared components, the split saves less. Measure your bundle before and after to know if the split is worth the added complexity.






Read the original article on Renderlog.in:



Text Tools:

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
3 Quellen
GPT-6 Astra Release Today? OpenAI’s Next Major AI Model Is Almost Here
1 Quelle
Apple accuses OpenAI of destroying evidence as trade-secrets fight intensifies
1 Quelle
Major AI platforms go down in unprecedented simultaneous outage
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten React.lazy() Route Code Splitting Explained

Thematisch verwandte Begriffe: Reactlazy, Route, Code, Splitting · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...