Sub topic 16
Code splitting by route
This chapter assumes you know how to define route objects, how loaders work, and what a dynamic import() does. You should also be comfortable with createBrowserRouter.
Why split by route
A bundler normally ships all your code in one file. The browser downloads every page's code before the first page appears. Routes are natural split points, because a user only needs the code for the page they are on. Splitting by route gives you smaller initial downloads and loads other pages on demand.
The `lazy` property
A route object accepts a lazy function. It returns a promise for a module. The router calls it when the URL starts matching that route, then copies the module's exports onto the route.
import { createBrowserRouter } from "react-router-dom";
const router = createBrowserRouter([
{
path: "/reports",
lazy: () => import("./routes/reports"),
},
]);
// routes/reports.jsx exports: export function Component() { ... }
// and may also export: export async function loader() { ... }The module can export Component, loader, action, errorElement (or ErrorBoundary), and other route properties. Use Component rather than element, because the module exports a component function, not a rendered element.
What happens during navigation
The router loads the module before it renders the route. If the route has a loader, the router fetches the code first and then runs the loader. During this time useNavigation().state is "loading", so your pending UI works the same way as it does for data loading.
Common errors
- Exporting the wrong names. If the module has a default export only, the router finds no
Componentand the route renders nothing useful. Use named exports. - Defining the same property twice. Do not set
elementon the route object and also exportComponentfrom the lazy module. Keep the property in one place. Static properties likepathandchildrenstay on the route object. - Splitting everything. Each lazy route adds a network request on navigation. Split large or rarely visited pages, and keep small, common pages in the main bundle.
- Import paths the bundler cannot analyze. Use a literal string in
import(). A path built at runtime may not produce a separate chunk.
Mixing lazy and static routes
You can make only some routes lazy. Here the home page ships in the main bundle, and the heavy reports page loads on demand.
import { createBrowserRouter } from "react-router-dom";
const router = createBrowserRouter([
{ path: "/", element: <h1>Home</h1> },
{ path: "/reports", lazy: () => import("./routes/reports") },
]);Check the result in your browser's network tab. Open the home page, then click a link to /reports. A new JavaScript file should appear at that moment.
Exercise
A route is defined as { path: "/reports", lazy: () => import("./routes/reports") }. The module routes/reports.jsx has only export default function Reports() { ... }. The page does not show the component. What should you change?
Show answer
The router reads named exports, not the default export. Change the module to export function Component() { ... }. You can also export loader or action from the same module.