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) orpointerdown. 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.
staleTimeon the query cache (React Query/SWR) for data that can be a few seconds old; HTTPCache-Controlon 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.allinside 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?
Keep reading
Related articles from the SudoSecurity blog.
What is Vite? The Build Tool That Made Frontend Development Fast Again
Vite replaced Webpack for a lot of teams without much fanfare. Native ES modules, a dev server that starts in milliseconds, and a production build that doesn't require understanding 300 lines of config. Here's how it works and why the switch is worth it.
PHP API Performance: N+1 Queries, cURL, and Connection Pools
A PHP API case study: fixing an N+1 query, twenty serial cURL calls, missing connection pooling, and stats recalculated on every request with Redis caching.
Inside TypeScript 7: What a Year-Long Rewrite in Go Actually Bought Developers
A deep look at how Microsoft's Go-based TypeScript 7 compiler achieves its speed gains, and what the Visual Studio Code team's year-long migration reveals about adopting it safely.
Node.js vs Deno vs Bun: Picking the Right JavaScript Runtime in 2026
Three JavaScript runtimes. Three different philosophies on what the developer experience should feel like. Here's how they compare on performance, ecosystem, tooling, and the only decision that actually matters for production.