Course contentsShow
← React
Lesson 2288 of 2,39650. Performance: Optimization TechniquesFree lesson

Route-Based Splitting Strategy

Split at route boundaries for maximum impact with minimal complexity.

What You'll Learn

You'll learn why splitting your code at route boundaries is the most effective place to start with code splitting — delivering immediate size wins with minimal architectural complexity.

Why Routes Are the Natural Split Point

Each route in your application represents a distinct user destination. When a user visits /dashboard, they don't need the code for /settings or /profile. Routes are already isolated by navigation — users can only be in one place at a time.

This makes routes the lowest-hanging fruit for code splitting. You get maximum bundle size reduction without the cognitive overhead of splitting at arbitrary component boundaries.

The Pattern

Instead of importing route components statically, use dynamic imports with React.lazy:

import { lazy } from 'react';

// ❌ Static imports load everything upfront
// import Dashboard from './Dashboard';
// import Settings from './Settings';

// ✅ Dynamic imports split by route
const Dashboard = lazy(() => import('./Dashboard'));
const Settings = lazy(() => import('./Settings'));

In your router configuration:

<Suspense fallback={<Spinner />}>
  <Routes>
    <Route path="/dashboard" element={<Dashboard />} />
    <Route path="/settings" element={<Settings />} />
  </Routes>
</Suspense>

Now each route loads only when navigated to. Your initial bundle shrinks dramatically because it only contains the home page code.

Why This Works So Well

Think of routes like chapters in a book. You don't need Chapter 10 in memory while reading Chapter 3. Routes naturally align with user intent — they're already thinking "I want to go to Settings" — so a brief loading state feels acceptable.

Unlike component-level splitting (which can cause jarring mid-page loading states), route splits happen during navigation when users expect a transition.

Key Takeaway: Start code splitting at route boundaries first — you'll capture 60-80% of the potential bundle size savings with straightforward, predictable splits that align with user navigation patterns.