MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Route-Based Code Splitting with React lazy0%
Study Materials
React.js for AdvancedProgress 0%
1.1Control Props and Inversion of Control Pattern1.2State Reducer Pattern in Advanced React1.3Prop Getters and Flexible Design Systems1.4Headless UI Architecture and Accessibility Primitives
6.1Route-Based Code Splitting with React lazy6.2Component-Level Lazy Loading with Suspense6.3Dynamic Imports and Prefetching Strategies6.4Analyzing Bundle Sizes with Rollup and Webpack Visualizer
HomeTutorialsReact.js for AdvancedRoute-Based Code Splitting with React lazy
Component-Level Lazy Loading with Suspense

Route-Based Code Splitting with React lazy

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

In monolithic single-page applications (SPAs), bundling an entire codebase into a single monolithic JavaScript payload degrades Initial Page Load performance, causing high First Contentful Paint (FCP) and Time to Interactive (TTI) scores. Route-based code splitting dynamically segments your JavaScript application into separate chunks loaded strictly on demand when a user navigates to a corresponding URL.


1. The Need for Route-Based Code Splitting

When Webpack, Vite, or Rollup bundles your application without splitting, every component, charting library, date-math utility, and admin module is sent to the client browser on the very first HTTP request. Even if the user only visits a simple landing page, they pay the bandwidth, decompression, and parsing cost for 50 other internal dashboards.

1Monolithic Bundle:
2[ Landing Page | Analytics Engine | D3 Charts | User Settings | Billing & Invoicing | Admin Portal ]
3Size: 3.8 MB (Loaded all at once on initial visit)
4 
5Route-Split Bundles:
6[ Core Runtime + Shared Vendors ] (220 KB, initial load)
7 ├── Navigates to /analytics ──> [ analytics.chunk.js ] (450 KB)
8 ├── Navigates to /billing ──> [ billing.chunk.js ] (180 KB)
9 └── Navigates to /admin ──> [ admin.chunk.js ] (320 KB)

2. Dynamic Imports and React.lazy()

React.lazy() lets you render a dynamic import as a regular React component. It takes a function that must call a dynamic import(), which returns a Promise that resolves to a module with a default export containing a React component.

1import React, { Suspense, lazy } from "react";
2import { BrowserRouter, Routes, Route, Link } from "react-router-dom";
3 
4// Lazy-loaded route components
5const DashboardPage = lazy(() => import("./pages/DashboardPage"));
6const AnalyticsPage = lazy(() => import("./pages/AnalyticsPage"));
7const SettingsPage = lazy(() => import("./pages/SettingsPage"));
8 
9// Suspense fallback skeleton
10function PageLoadingSkeleton() {
11 return (
12 <div className="flex h-screen items-center justify-center bg-slate-900 text-white">
13 <div className="flex flex-col items-center gap-3">
14 <div className="h-10 w-10 animate-spin rounded-full border-4 border-cyan-500 border-t-transparent" />
15 <p className="text-sm font-medium text-slate-400">Loading module chunk...</p>
16 </div>
17 </div>
18 );
19}
20 
21export function AppRouter() {
22 return (
23 <BrowserRouter>
24 <nav className="flex gap-4 border-b border-slate-800 bg-slate-950 p-4 text-cyan-400">
25 <Link to="/">Dashboard</Link>
26 <Link to="/analytics">Analytics</Link>
27 <Link to="/settings">Settings</Link>
28 </nav>
29 
30 {/* Suspense boundary wrapping lazy route rendering */}
31 <Suspense fallback={<PageLoadingSkeleton />}>
32 <Routes>
33 <Route path="/" element={<DashboardPage />} />
34 <Route path="/analytics" element={<AnalyticsPage />} />
35 <Route path="/settings" element={<SettingsPage />} />
36 </Routes>
37 </Suspense>
38 </BrowserRouter>
39 );
40}

3. Handling Named Exports with React.lazy()

React.lazy requires that the dynamically imported module has a default export. If a library or design system uses named exports exclusively, you must intermediate the Promise resolution:

1// Importing named export { AdminAnalytics } from module
2const AdminAnalytics = lazy(() =>
3 import("./modules/AdminDashboard").then((module) => ({
4 default: module.AdminAnalytics,
5 }))
6);

4. Chunk Naming and Build Configurations

In Vite (using Rollup) or Webpack, configure manualChunks or magic comments to generate predictable chunk names for optimal cache invalidation.

1// vite.config.ts
2import { defineConfig } from "vite";
3import react from "@vitejs/plugin-react";
4 
5export default defineConfig({
6 plugins: [react()],
7 build: {
8 rollupOptions: {
9 output: {
10 manualChunks(id) {
11 if (id.includes("node_modules")) {
12 if (id.includes("recharts") || id.includes("d3")) {
13 return "vendor-charts";
14 }
15 if (id.includes("react-router-dom")) {
16 return "vendor-router";
17 }
18 return "vendor-core";
19 }
20 },
21 },
22 },
23 },
24});

5. Summary Best Practices

  1. 1
    Top-Level Route Splitting: Split at top-level navigation routes first; this delivers the highest return on investment for FCP reduction.
  2. 2
    Granular Suspense Boundaries: Avoid wrapping the entire application in a single Suspense fallback that hides navigation chrome; wrap individual route views inside a stable layout.
  3. 3
    Chunk Prefetching: Prefetch chunks on link hover or intent to reduce navigation latency before the click occurs.

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What contract must the function passed to React.lazy() satisfy?
Question 2
What happens if a React.lazy component renders without an ancestor Suspense component?
Question 3
How can you load a named export { SettingsModal } using React.lazy()?
Question 4
Why is route-based code splitting typically prioritized over fine-grained component splitting?
Question 5
What is the primary benefit of configuring manualChunks in Rollup/Vite for route-split apps?
Next Lesson

Component-Level Lazy Loading with Suspense

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Next.js App Router and Server-First ParadigmsComponent-Level Lazy Loading with Suspense

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master React.js for Advanced with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext