Course map
Stage 1: App Router Foundations
Stage 2: The Rendering Model
Stage 3: Data, Caching and Mutations
Stage 4: Full-Stack Next.js
Stage 5: Production and Specialization

Part 09 / Topic 4 of 5

Fetch deduplication vs React's cache() — verified, not assumed

Next.js's own docs (node_modules/next/dist/docs/01-app/01-getting-started/06-fetching-data.md in this install) state plainly: "Identical fetch requests in a React component tree are memoized by default." Reading the mechanism behind that claim, in this exact install's own source rather than taking the doc's word for it: node_modules/next/dist/server/lib/patch-fetch.js requires ./dedupe-fetch and wraps globalThis.fetch in createDedupeFetch(...) — the deduplication is a property of the patched global fetch function itself, applied to it specifically, not a general behavior of "any awaited call in a render pass."

Mandi Fresh's data sources are plain local async functions, not fetch() calls — the entire point of this part's "no real network" setup. So this automatic memoization does not apply to them, and that is directly checkable, not a guess: stock.ts keeps a per-request call counter (via React's own cache(), used here just to get a fresh counter object per request):

// harness/app/mandi/lib/stock.ts
export const warehouseCallsThisRequest = cache(() => ({ count: 0 }));

async function checkWarehouse(productId: string): Promise<StockInfo> {
  warehouseCallsThisRequest().count++;
  await wait(350);
  return { productId, unitsAvailable: unitsByProduct[productId] ?? 0 };
}

export const getStock = checkWarehouse;              // NOT deduplicated
export const getStockCached = cache(checkWarehouse);  // deduplicated

Two near-identical pages both read the same product's stock twice — once for a "badge," once for a "low stock" note — the way two unrelated widgets on a real page legitimately might:

// harness/app/mandi/dedupe/uncached/page.tsx (the async Panel component)
const badge = await getStock("g1");
const warning = await getStock("g1");
const calls = warehouseCallsThisRequest().count;

Run for real against this harness:

$ curl -s http://localhost:PORT/mandi/dedupe/uncached
...
<li id="calls">Warehouse calls this request: 2</li>

Swapping only the function (getStockCached instead of getStock), nothing else:

$ curl -s http://localhost:PORT/mandi/dedupe/cached
...
<li id="calls">Warehouse calls this request: 1</li>

2 calls vs 1, same page shape, same two reads, same product id — the only difference is whether cache() wraps the function. This is the exact fix the reference curriculum's "React cache() for other data sources" lesson describes, now proven against a function this course wrote and ran, not copied from a doc.

🔍 Going deeper — a nuance worth not overclaiming: Because both reads in the example above run through Promise.all and each is its own independent 350ms timer, the uncached version is not meaningfully slower in wall-clock time than the cached one — two overlapping 350ms timers still finish in about 350ms, cached or not. cache()'s real win here is not speed, it is halving the number of calls a real backend would actually receive — the difference between one warehouse query and two for the same page view. A database or a billed third-party API cares about that count a great deal even when the clock doesn't show it. Don't assume "the cached version will always load visibly faster" — measure what you're actually trying to save.

Full files: examples/03-stock-module.ts, examples/04-dedupe-uncached.tsx, examples/05-dedupe-cached.tsx.

Done here? The practice set for Part 9 — including the solutions — is on the part page.

Search the guide