Skip to content
Tutorials Center
My learning

Topics

Lesson 32 of 39 ยท Streaming and Performance

Bundle Size and Performance

Make Remix and React Router apps fast: route-based code splitting, server-only modules, trimming loader data, parallel data fetching, image handling and measuring Core Web Vitals.

Performance work is easier when you know where time goes. In a framework-mode app a page load has four parts: the server runs loaders and renders HTML, the network carries bytes, the browser parses HTML, CSS and JavaScript, and React hydrates the page. Streaming and caching, covered earlier, attack the first two. This lesson covers the rest, with the habits that matter most and a way to measure so you do not guess.

Route-based code splitting is automatic

Every route module becomes its own JavaScript chunk. A visitor to /pricing downloads the code for the root, the pricing route and shared libraries only, not the admin dashboard. That is the biggest win and it is free. You keep it by not importing heavy code into the root route or a shared component that every page uses.

For a heavy component used by one screen (a chart, a code editor), lazy-load it so it joins the route's chunk only when needed:

TSX
// read-only, React Router framework mode
import { lazy, Suspense } from "react";

const Chart = lazy(() => import("~/components/chart")); // separate chunk

export default function Reports() {
  return (
    <Suspense fallback={<div style={{ height: 320 }} />}>
      <Chart />
    </Suspense>
  );
}

Keep server code out of the browser

Files named *.server.ts (or inside a .server folder) are guaranteed to be excluded from the client build; importing one from client code is a build error, which protects both secrets and size. Heavy server-only libraries (database clients, Markdown processors, PDF generators) belong there. Likewise, work done in a loader is free for the browser: parse the Markdown on the server and send HTML, instead of shipping a Markdown parser to every visitor.

Return less data

Everything a loader returns is serialized into the HTML and, on later navigations, into a data response. Returning an entire database row when the page shows one field wastes bytes and can leak private columns.

Node.js Stand-in: measure what a loader returns

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Fetch in parallel

Awaiting calls one after another turns delays into a sum. When the calls do not depend on each other, start them together:

TypeScript
// slow: 120 ms + 90 ms = 210 ms
const user = await getUser(id);
const orders = await getOrders(id);

// fast: about 120 ms, the slower of the two
const [user, orders] = await Promise.all([getUser(id), getOrders(id)]);

Nested routes already run their loaders in parallel, so splitting data across the layout and the page (each in its own loader) gives you parallelism for free. Avoid waterfalls: a child component that fetches in an effect after it renders always starts late. Prefer loaders.

Node.js Stand-in: sequential versus parallel loaders

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Hydration cost

Hydration cost grows with the amount of interactive JavaScript on the page. Habits that keep it low: render lists with stable keys and modest length (paginate or virtualize long lists), memoize expensive derived data computed on every render, and avoid shipping large JSON that the page does not display. A page of mostly static content can ship very little script if the interactive parts are small.

Images, fonts and CSS

  • Give every <img> explicit width and height (or an aspect-ratio) so the layout does not jump, use loading="lazy" below the fold and fetchpriority="high" on the one hero image.
  • Serve modern formats (AVIF, WebP) in the sizes you display, using srcset.
  • Self-host fonts with font-display: swap and preload only the one or two files used above the fold.
  • Export CSS through the route links function so only the CSS for matched routes loads.

Measure, then fix

Use real numbers, not feelings:

Metric What it measures Good target
LCP (Largest Contentful Paint) when the main content appears under 2.5 s
INP (Interaction to Next Paint) responsiveness to clicks and keys under 200 ms
CLS (Cumulative Layout Shift) unexpected movement under 0.1
TTFB (Time to First Byte) server and network speed under 0.8 s

Lighthouse in Chrome DevTools gives lab numbers; real-user monitoring (the web-vitals library sending data to your analytics) tells you what visitors experience. For bundle analysis, a visualizer plugin for your bundler shows which package takes the space. Fix the largest item first: usually one big dependency, an unoptimized hero image or a slow loader. Add a Server-Timing header to loaders to see which query is slow.

A short checklist

  1. Await only what shapes the first paint; stream the rest.
  2. Cache public pages at the CDN.
  3. Return minimal loader data; keep heavy libraries in .server files.
  4. Run independent fetches in parallel.
  5. Size images, preload one hero asset, split heavy components.
  6. Prefetch likely next links.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What gives each route its own JavaScript chunk?

    Route-based splitting is built in. You protect it by not importing heavy code into the root or shared components.
  2. 2. Why return only the fields a page needs from a loader?

    Smaller payloads download and parse faster and keep private columns off the wire.
  3. 3. Two independent calls take 120 ms and 90 ms. How long does Promise.all take, roughly?

    Parallel calls take as long as the slowest one.
  4. 4. Which metric measures unexpected layout movement?

    Cumulative Layout Shift. Reserve space for images and late content to keep it low.

Summary

  • Route chunks split automatically; keep heavy code out of shared modules and the root.
  • Use .server files for server-only code; return minimal loader data.
  • Fetch independent data in parallel and avoid effect-based waterfalls.
  • Size images, preload carefully, and measure with Lighthouse and real-user vitals.

Next: Testing Loaders and Actions