awesome-everything RU
↑ Back to the climb

Frontend Architecture

Code splitting: code reading

Crux Read real React/JS splitting snippets — a dynamic-import boundary, a lazy+Suspense layout, and a prefetch-on-hover handler — and pick the behaviour or the highest-leverage fix.
Your altitude — climbing toward senior
ZeroJuniorMiddleSenior
You are at senior altitude — in orbit
◷ 13 min

Splitting bugs live in where the boundary sits and when the chunk is requested. Read each snippet, trace the request timing the browser actually performs, and choose the fix a senior would make first.

Goal

Practise reading split points the way you debug them in production: find where the dynamic import boundary really is, see when Suspense triggers a fetch, and spot the waterfall a hint would pre-empt.

Snippet 1 — where the boundary sits

// A: import at module top — bundled into the parent chunk
import { HeavyEditor } from "./HeavyEditor";

function EditPanel({ open }) {
  if (!open) return null;
  return <HeavyEditor />;
}

// B: dynamic import inside the gate
const HeavyEditor = React.lazy(() => import("./HeavyEditor"));

function EditPanel({ open }) {
  if (!open) return null;
  return (
    <Suspense fallback={<Spinner />}>
      <HeavyEditor />
    </Suspense>
  );
}
Quiz

The editor is 200 KB and only opens when the user clicks Edit. What does each version actually ship, and which is right here?

Snippet 2 — nested Suspense boundaries

const Dashboard = React.lazy(() => import("./Dashboard"));
const Chart = React.lazy(() => import("./Chart"));

function Page() {
  return (
    <Suspense fallback={<PageSkeleton />}>
      <Dashboard>
        <Suspense fallback={<ChartSpinner />}>
          <Chart />
        </Suspense>
      </Dashboard>
    </Suspense>
  );
}
Quiz

On a 150 ms RTT phone, in what order are the Dashboard and Chart chunks fetched, and why does that matter?

Snippet 3 — prefetch on hover

const Settings = React.lazy(() => import("./Settings"));

function NavLink() {
  return (
    <a
      href="/settings"
      onMouseEnter={() => import("./Settings")}
    >
      Settings
    </a>
  );
}
Quiz

What does the onMouseEnter handler accomplish, and what is the failure mode if you drop it?

Recap

Three things decide splitting behaviour in code: a static top-level import is always bundled into the parent chunk, so only import() creates a split point — and a runtime condition does not change that. Nested React.lazy boundaries are discovered sequentially, so each level adds a round trip on a high-RTT link. And firing import() early (on hover or idle) warms the chunk cache so the eventual mount skips the waterfall and the spinner. Read where the boundary sits and when the fetch is triggered, then move the trigger earlier rather than merging the chunk back in.

Continue the climb ↑Code splitting: fix the waterfall
shortcuts expand
search
K
prev piece
k
next piece
j
cycle tier
t
this menu
?
sources2
expand
  1. 01
  2. 02

Trademarks belong to their respective owners. Editorial reference only.