Skip to content

Lesson 10 of 14 · 18m

Data Fetching Without Waterfalls

Serial client-side fetches stack round trips — route loaders, parallel Promise.all, intent prefetch, and streaming turn the waterfall into a firehose.

Open a client-rendered app's network waterfall on a slow page and you will usually find the same picture: HTML, then the JS bundle, then GET /api/me, then GET /api/cart, then GET /api/recommendations — four serial round trips where each request waits for the one before it to come back, get parsed, and trigger an effect that issues the next. On a 250 ms RTT that's a full second of latency added after TTFB, and it compounds: GET /api/me unblocks a component that fetches /api/preferences, which unblocks a component that fetches /api/items. This is the fetch waterfall, and it's the reason client-rendered apps feel slow even with fast servers and small bundles.

The wrong mental model is "the API is slow." Each endpoint is probably fast. The problem is they run one after another because the code accidentally serializes them — child components can't fetch until their parents render, and parents can't render until their fetch resolves.

Three ways waterfalls form

  • Render-then-fetch. A component mounts, an effect runs, fetch starts. Children deeper in the tree repeat it. The depth of your component tree becomes your latency multiplier.
  • Sequential awaits. const user = await getUser(); const cart = await getCart() — two independent requests served serially because of two lines of code.
  • Conditional fetching. Data loads only after some state becomes true, so the request can't even start until round-trip one finishes.

Hoist the fetches to the route

Route loaders exist to solve exactly this: declare a route's data needs up front, and the router fires all of them in parallel while the code bundle is still downloading.

// TanStack Router — fetches start on navigation intent, not on mount
export const Route = createFileRoute("/product/$id")({
  loader: ({ params }) =>
    Promise.all([                     // parallel, not serial
      fetch(`/api/products/${params.id}`).then((r) => r.json()),
      fetch(`/api/products/${params.id}/stock`).then((r) => r.json()),
      queryClient.prefetchQuery(reviewsQuery(params.id)),
    ]),
  component: ProductPage,
});

React Router's loader, Next's route loading.tsx + parallel server-side fetches, and SvelteKit's load all do the same job: fetch before render, in parallel, at the route boundary. The pattern has a name — render-as-you-fetch — and the invariant is: no component waits for a fetch that could have started at navigation time.

When you're stuck inside a component anyway, the primitive is still Promise.all. Any two requests that don't depend on each other's output belong in the same tick.

Then remove the wait entirely

  • Prefetch on intent. Fire the route loader on pointerover/focus (a 150–300 ms head start) or pointerdown. React Query: queryClient.prefetchQuery(...). The user's hover time is free latency.
  • Collapse endpoints. Five small API calls → one route-level endpoint (a BFF, a GraphQL query, a loader-side join) turns N serial RTTs into one. The server can make those five calls to internal services over the datacenter LAN at single-digit-ms cost.
  • Stream the slow part. Suspense + deferred data lets the page paint its shell and fast content while a slow query keeps loading — turn a waterfall tail into a progressive render instead of a blocked screen.
  • Cache at the right layer. staleTime on the query cache (React Query/SWR) for data that can be a few seconds old; HTTP Cache-Control on the API responses themselves for anything shareable.

Field notes

  • Watch for the "spinner cascade" smell in the UI: nested loading skeletons are nested waterfalls wearing a costume.
  • A Promise.all inside an effect still starts after mount. It fixes serialization, not the render-then-fetch delay — hoist to the loader anyway.
  • GraphQL doesn't fix waterfalls by itself; a client that issues three sequential queries has the same problem with a different wire format.
  • Server Components eliminate the client waterfall for server-only data entirely — the fetch happens server-side where RTTs are free-ish. That's the deepest version of the same fix.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.