Mid-level 9 min · April 12, 2026

Next.js 16 Caching — Stale Prices from Full Route Cache

Product prices went stale for 24 hours when Full Route Cache baked a force-cached fetch without revalidation.

N
Naren · Founder
Plain-English first. Then code. Then the interview question.
About
 ● Production Incident 🔎 Debug Guide ⚙ Triage Commands
Quick Answer
  • Next.js 16 has four server caching layers: Full Route Cache, Partial Prerendering, Data Cache (fetch), and React cache()
  • Plus a client-side Router Cache that stores RSC payloads (30s for static, 5min for dynamic)
  • Full Route Cache stores entire rendered pages at build time — only when all data is cacheable
  • Partial Prerendering splits a page into a static shell + dynamic streaming holes — one strategy per component
  • fetch() defaults to cache: 'no-store' in Next.js 15+ — you must opt-in with revalidate or cache: 'force-cache'
  • React cache() deduplicates identical fetches within a single render pass — not a persistent cache
  • Biggest mistake: assuming fetch() caches by default — in Next.js 16 it doesn't, you must opt in
✦ Definition~90s read
What is Next.js 16 Caching — Stale Prices from Full Route Cache?

Next.js caching is a multi-layered system that determines how your pages and data are stored and served, directly impacting both performance and freshness. The Full Route Cache (Layer 1) is the most aggressive: it pre-renders entire pages at build time into static HTML, meaning every visitor gets the same pre-built file.

Next.js 16 caches data and pages at multiple layers.

This is fantastic for marketing sites or blogs where content rarely changes, but it becomes a liability for dynamic data like pricing—if a product's price updates after deployment, users see stale values until the next build. This is why e-commerce sites using static generation often serve outdated prices, a classic pitfall when caching isn't tuned to data volatility.

Below the route cache, Partial Prerendering (Layer 2) lets you mix static shells with dynamic holes—your page layout is cached, but specific components (like a price ticker) fetch fresh data per request. The fetch() cache (Layer 3) gives you per-request control via next: { revalidate } or cache: 'no-store', letting you decide how long a specific API response lives.

Finally, React's cache() function (Layer 4) deduplicates identical requests within a single render pass, preventing redundant network calls without affecting cross-request freshness. The key insight: these layers stack, and the most restrictive one wins.

If your Full Route Cache is static, even a no-store fetch won't help—the page itself is already baked. For real-time prices, you need to either disable the route cache entirely (using dynamic = 'force-dynamic') or use Partial Prerendering to isolate the dynamic data.

Tools like Vercel's ISR (Incremental Static Regeneration) or on-demand revalidation can bridge the gap, but the fundamental tradeoff remains: cache aggressively for speed, but invalidate ruthlessly for accuracy.

Plain-English First

Next.js 16 caches data and pages at multiple layers. Think of it like a shipping warehouse: Full Route Cache is the pre-packed box ready to ship (fastest, but contents only change when you rebuild or invalidate). Partial Prerendering is like a pre-built picture frame with a live video playing inside — the frame is instant, the content streams. fetch() cache decides how fresh each item should be — by default it fetches fresh every time, you opt-in to keep it. React cache() ensures you don't fetch the same item twice during one packing session. Getting these layers wrong means stale data, slow pages, or both.

Next.js 16 ships with four distinct server caching layers plus a client Router Cache. Each operates at a different level — build time, request time, component render time — and each has different invalidation semantics. Engineers who treat them as interchangeable produce either stale pages or unnecessarily slow ones.

The core change in Next.js 15: fetch() no longer caches by default. In Next.js 13/14, fetch() defaulted to force-cache, causing widespread stale data bugs. In Next.js 16, fetch() defaults to no-store — data is fresh on every request unless you explicitly opt into caching with next: { revalidate } or cache: 'force-cache'.

The correct approach: understand what each layer caches, when it invalidates, and how to opt in selectively. This article breaks down all four layers with production patterns, debugging commands, and the failure scenarios that catch teams off guard.

Why Full Route Cache Can Serve Stale Prices

Next.js 16 caching strategies define how your application stores and reuses rendered pages, data, and static assets to balance performance with freshness. The core mechanic is the Full Route Cache (FRC), which caches entire HTML pages at build time or on first request, then serves them instantly for subsequent visitors. This cache is static by default — it does not revalidate unless you explicitly configure ISR (Incremental Static Regeneration) or opt into dynamic rendering. In practice, FRC is a key-value store keyed by route path, storing the final HTML output. It operates at the edge or server level, with a default TTL of infinity for static routes. The critical property: FRC does not automatically invalidate when underlying data changes. If your product prices update in the database, the cached HTML still shows the old price until you trigger revalidation via revalidatePath, revalidateTag, or a time-based revalidate interval. Use FRC when your content changes infrequently and you need sub-millisecond response times — think marketing pages, blog posts, or documentation. It matters in real systems because it directly impacts revenue: a stale price on an e-commerce product page can lead to customer disputes, lost sales, or regulatory fines. The tradeoff is always between latency and freshness, and FRC biases heavily toward latency.

Stale Data Is Silent
Full Route Cache does not warn you when it serves stale content — you must explicitly configure revalidation or use on-demand invalidation to prevent price discrepancies.
Production Insight
E-commerce product page shows yesterday's price after a flash sale update because FRC was not invalidated.
Users add items to cart at the cached price, then checkout fails with a 'price changed' error, causing cart abandonment and support tickets.
Always pair static routes with revalidate or revalidateTag for any page displaying dynamic data like prices, inventory, or user-specific content.
Key Takeaway
Full Route Cache is static by default — it never revalidates unless you tell it to.
Stale data from FRC is invisible to monitoring; you must test cache invalidation paths explicitly.
Use ISR or dynamic rendering for any route where data freshness has business impact.

Layer 1: Full Route Cache (Build-Time Static Generation)

Full Route Cache stores the entire rendered HTML and RSC payload at build time. When a user requests a statically generated page, Next.js serves the cached HTML directly — no server-side rendering, no data fetching, no React rendering. This is the fastest possible response.

Pages enter the Full Route Cache when all data fetching opts into caching (using fetch with cache: 'force-cache' or next: { revalidate: N }) and the page has no dynamic dependencies (no cookies(), headers(), or fetch with no-store). The cache key is the route path.

Invalidation happens in three ways: a new build (redeploy), time-based revalidation (revalidate: N seconds), or on-demand revalidation (revalidatePath() or revalidateTag() called from a route handler or server action). Without explicit revalidation, cached pages persist indefinitely until the next build.

io.thecodeforge.nextjs.full-route-cache.tsxTSX
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// ============================================
// Full Route CacheStatic Generation Patterns
// ============================================

// ---- Default in Next.js 16: fetch is NOT cached ----
// This page is dynamic by default — fresh data every request

// File: app/products/[slug]/page.tsx

interface Product {
  id: string
  slug: string
  name: string
  price: number
  description: string
}

// This fetch uses the default: cache: 'no-store' — fresh every request
async function getProductFresh(slug: string): Promise<Product> {
  const res = await fetch(`https://api.example.com/products/${slug}`)
  return res.json()
}

// ---- Opt-in to permanent cache: force-cache ----
// Use for truly static content (rarely)

async function getProductStatic(slug: string): Promise<Product> {
  const res = await fetch(`https://api.example.com/products/${slug}`, {
    cache: 'force-cache', // Opt-in: cache permanently until revalidate
  })
  return res.json()
}

// ---- Opt-in to ISR: stale-while-revalidate ----
// Serve cached data, regenerate in background after N seconds

async function getProductWithRevalidation(slug: string): Promise<Product> {
  const res = await fetch(`https://api.example.com/products/${slug}`, {
    next: { revalidate: 300 }, // Regenerate every 5 minutes
  })
  return res.json()
}

// ---- On-demand revalidation: webhook-triggered ----

// File: app/api/revalidate/route.ts
import { revalidatePath, revalidateTag } from 'next/cache'
import { NextRequest, NextResponse } from 'next/server'

export async function POST(request: NextRequest) {
  const { path, tag, secret } = await request.json()

  if (secret !== process.env.REVALIDATION_SECRET) {
    return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
  }

  if (path) {
    revalidatePath(path)
    return NextResponse.json({ revalidated: true, path })
  }

  if (tag) {
    revalidateTag(tag)
    return NextResponse.json({ revalidated: true, tag })
  }

  return NextResponse.json({ error: 'Provide path or tag' }, { status: 400 })
}

// ---- Tagged fetch: revalidate groups of data ----

async function getProductByTag(slug: string): Promise<Product> {
  const res = await fetch(`https://api.example.com/products/${slug}`, {
    next: {
      revalidate: 3600,
      tags: ['products', `product-${slug}`],
    },
  })
  return res.json()
}

// ---- generateStaticParams: pre-generate known routes ----

export async function generateStaticParams() {
  const products = await fetch('https://api.example.com/products', {
    cache: 'force-cache',
  }).then((res) => res.json())

  return products.map((product: Product) => ({
    slug: product.slug,
  }))
}
Full Route Cache = Pre-Built HTML
  • fetch() does NOT cache by default in Next.js 16 — add next: { revalidate } or cache: 'force-cache' to opt in
  • revalidate: N serves stale data immediately, regenerates in background — stale-while-revalidate pattern
  • On-demand revalidation via revalidatePath() or revalidateTag() — triggered by webhooks, not timers
  • Tagged fetch groups multiple data sources — revalidateTag('products') invalidates all tagged fetches
  • generateStaticParams pre-generates known routes — pages are built at build time only if data is cacheable
Production Insight
fetch() defaults to no-store in Next.js 16 — data is fresh unless you explicitly opt into caching.
Without revalidate or on-demand invalidation, Full Route Cache pages persist until next deploy.
Rule: always set next: { revalidate } or cache: 'force-cache' when you want caching — never assume it.
Key Takeaway
Full Route Cache serves pre-built HTML — the fastest response, but only when you opt data into caching.
Three invalidation methods: redeploy, time-based revalidate: N, on-demand revalidatePath/revalidateTag.
Rule: CMS-driven content needs webhook-triggered revalidation — time-based alone causes stale windows.

Layer 2: Partial Prerendering (Static Shell + Dynamic Holes)

Partial Prerendering (PPR) splits a page into a static shell and dynamic streaming holes. The static shell is pre-rendered at build time and served instantly. Dynamic parts are wrapped in Suspense boundaries and streamed in as their data resolves.

PPR is Next.js 16's default. Instead of choosing between fully static and fully dynamic, PPR lets you mix both on the same page. The header, navigation, and layout are static. The personalized content, real-time data, and user-specific elements stream in.

The key constraint: dynamic content must be wrapped in a Suspense boundary AND use uncached data (fetch with no-store, cookies(), headers()). With PPR, a dynamic API only forces that Suspense boundary to stream — it does not make the entire page dynamic.

io.thecodeforge.nextjs.partial-prerendering.tsxTSX
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// ============================================
// Partial PrerenderingStatic Shell + Dynamic Streaming
// ============================================

// File: app/dashboard/page.tsx
import { Suspense } from 'react'

// ---- Static shell: pre-rendered at build time ----

function DashboardHeader() {
  return (
    <header className="border-b p-4">
      <h1 className="text-2xl font-bold">Dashboard</h1>
      <nav className="mt-2 flex gap-4">
        <a href="/dashboard/overview">Overview</a>
        <a href="/dashboard/analytics">Analytics</a>
      </nav>
    </header>
  )
}

// ---- Dynamic holes: streamed with Suspense ----

async function RevenueCard() {
  const res = await fetch('https://api.example.com/revenue/today', {
    cache: 'no-store', // Dynamic — fresh every request
  })
  const data = await res.json()

  return (
    <div className="rounded-lg border p-4">
      <p className="text-sm text-muted-foreground">Today's Revenue</p>
      <p className="text-3xl font-bold">${data.total.toLocaleString()}</p>
    </div>
  )
}

async function ActivityFeed() {
  const res = await fetch('https://api.example.com/activity/recent', {
    cache: 'no-store',
  })
  const activities = await res.json()

  return (
    <div className="space-y-2">
      {activities.map((activity: any) => (
        <div key={activity.id} className="flex items-center gap-2 text-sm">
          <span>{activity.description}</span>
        </div>
      ))}
    </div>
  )
}

function RevenueSkeleton() {
  return <div className="h-24 animate-pulse rounded-lg bg-muted" />
}

// ---- Main page: static shell + Suspense-wrapped dynamic holes ----

export default function DashboardPage() {
  return (
    <div>
      <DashboardHeader />
      <main className="grid gap-4 p-4 sm:grid-cols-2">
        <Suspense fallback={<RevenueSkeleton />}> 
          <RevenueCard />
        </Suspense>
        <Suspense fallback={<RevenueSkeleton />}> 
          <ActivityFeed />
        </Suspense>
      </main>
    </div>
  )
}

// ---- With PPR: dynamic APIs only affect their boundary ----
// export default async function Page({ searchParams }) {
//   return (
//     <Suspense fallback={...}>
//       <ComponentUsingSearchParams searchParams={searchParams} />
//     </Suspense>
//   )
// }
PPR = Pre-Built Frame + Live Content
  • Static shell renders at build time — header, nav, layout are cached in Full Route Cache
  • Dynamic holes are wrapped in Suspense — they stream in as data resolves at request time
  • Fallback UI (skeletons) shows immediately while dynamic content loads — no blank screen
  • With PPR, dynamic APIs only force their Suspense boundary to stream — not the entire page
  • PPR is default in Next.js 16 — opt out with export const dynamic = 'force-dynamic'
Production Insight
PPR requires Suspense boundaries around every dynamic component — missing one delays the static shell.
With PPR enabled, cookies() or searchParams only make that component dynamic, not the whole page.
Rule: if the static shell is slow, check for uncached fetches at the root level outside Suspense.
Key Takeaway
PPR splits pages into static shells and dynamic streaming holes — the best of both worlds.
Dynamic content must be inside Suspense with no-store data — PPR streams only that boundary.
Rule: move all dynamic data fetching into Suspense-wrapped components.

Layer 3: fetch() Cache (Per-Request Data Freshness)

fetch() in Next.js extends the native Fetch API with caching options. In Next.js 15+, each fetch call defaults to cache: 'no-store' — fresh data on every request unless you opt in. You can independently control caching: permanently (force-cache), never (no-store, the default), or with time-based revalidation (revalidate: N).

This is the most granular caching layer. A single page can have five fetch calls with five different cache strategies. Product data might revalidate every 5 minutes. User data might never cache. Static configuration might cache permanently.

The critical nuance: fetch() caching only works in Server Components. In development, every request triggers a fresh fetch even with caching enabled. In production, the fetch result is stored in the Data Cache.

io.thecodeforge.nextjs.fetch-cache.tsxTSX
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// ============================================
// fetch() CachePer-Request Data Freshness Control
// ============================================

// ---- Cache options reference (Next.js 16) ----

// Option 1: no-store (DEFAULT)
// Data is fetched fresh on every request — never cached
async function getUserSession(token: string) {
  const res = await fetch('https://api.example.com/session', {
    cache: 'no-store',
    headers: { Authorization: `Bearer ${token}` },
  })
  return res.json()
}

// Option 2: force-cache
// Data is fetched once and cached permanently until revalidated
async function getSiteConfig() {
  const res = await fetch('https://api.example.com/config', {
    cache: 'force-cache',
  })
  return res.json()
}

// Option 3: revalidate (stale-while-revalidate)
// Serve cached data, regenerate in background after N seconds
async function getProductList() {
  const res = await fetch('https://api.example.com/products', {
    next: { revalidate: 300 }, // 5 minutes
  })
  return res.json()
}

// Option 4: Tagged revalidation
async function getProductsByCategory(category: string) {
  const res = await fetch(`https://api.example.com/products?category=${category}`, {
    next: {
      revalidate: 600,
      tags: ['products', `category-${category}`],
    },
  })
  return res.json()
}

// ---- Mixing cache strategies on one page ----

export default async function ProductPage({ params }: { params: { slug: string } }) {
  const config = await getSiteConfig() // cached permanently
  const product = await fetch(`https://api.example.com/products/${params.slug}`, {
    next: { revalidate: 300 },
  }).then(r => r.json())
  const pricing = await fetch(`https://api.example.com/pricing/${params.slug}`, {
    cache: 'no-store',
  }).then(r => r.json())

  return (
    <div>
      <h1>{product.name}</h1>
      <p>${pricing.effectivePrice}</p>
    </div>
  )
}
fetch() Defaults to no-store — Opt In to Cache
  • fetch() without options defaults to no-store in Next.js 16 — data is fresh every request
  • cache: 'force-cache' caches permanently — use for static content, add revalidation
  • revalidate: N serves stale data immediately, regenerates in background — sweet spot for most content
  • Tagged fetch enables group invalidation — revalidateTag('products') invalidates all tagged fetches
  • fetch() caching only works in Server Components — client components use native fetch
Production Insight
fetch() defaults to no-store in Next.js 16 — you must explicitly opt into caching.
Without next: { revalidate } or cache: 'force-cache', data is fetched fresh on every request.
Rule: every fetch() that should be cached must have an explicit cache option.
Key Takeaway
fetch() cache is per-request granularity — each call independently controls freshness.
Three options: no-store (default, fresh), force-cache (permanent), revalidate: N (stale-while-revalidate).
Rule: if data is slow, check if you forgot to add caching — the default is no-store, not force-cache.

Layer 4: React cache() (Request-Level Deduplication)

React cache() deduplicates identical function calls within a single render pass. It is not a persistent cache — it does not survive across requests or builds. It prevents the same data from being fetched multiple times when the same function is called from different components during one server render.

The common scenario: a layout and a page both need the current user. Without cache(), getUser() is called twice. With cache(), the second call returns the memoized result from the first call. The function must be defined at the module level — not inside a component.

io.thecodeforge.nextjs.react-cache.tsxTSX
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// ============================================
// React cache() — Request-Level Deduplication
// ============================================

import { cache } from 'react'

// ---- CORRECT: cache() at module level ----

export const getUser = cache(async (userId: string) => {
  console.log('Fetching user:', userId) // Logs once per unique userId per request
  const res = await fetch(`https://api.example.com/users/${userId}`, {
    cache: 'no-store',
  })
  return res.json()
})

export const getProduct = cache(async (slug: string) => {
  const res = await fetch(`https://api.example.com/products/${slug}`, {
    next: { revalidate: 300 },
  })
  return res.json()
})

// ---- Usage: multiple components, one fetch ----

// File: app/layout.tsx
import { getUser } from '@/lib/data'

export default async function RootLayout({ children }: { children: React.ReactNode }) {
  const user = await getUser('current') // First call — fetches
  return <html><body><nav>{user.name}</nav>{children}</body></html>
}

// File: app/dashboard/page.tsx
import { getUser } from '@/lib/data'

export default async function DashboardPage() {
  const user = await getUser('current') // Second call — returns cached result
  return <div>Welcome, {user.name}</div>
}
React cache() = Request-Scoped Memoization
  • cache() deduplicates identical calls within one server render — not across requests or builds
  • Must be defined at module level — defining inside a component creates a new cache per render
  • Combines with fetch() options: cache() for deduplication, fetch options for persistence control
  • Use when multiple components need the same data — layout + page + sidebar all calling getUser()
Production Insight
React cache() deduplicates within one render pass — it is not a persistent cache.
Defining cache() inside a component creates a new cache per render — no deduplication happens.
Rule: always define cached functions at module level in a separate data layer file.
Key Takeaway
React cache() is request-scoped memoization — deduplicates identical calls within one server render.
Must be at module level — component-level cache() creates a new instance per render.
Rule: combine cache() with fetch() options — deduplication within request, freshness across requests.

Choosing the Right Caching Strategy

The four caching layers are not interchangeable. Each solves a different problem at a different level. Choosing the wrong one produces either stale data or unnecessary server load.

The decision framework: how often does the data change, and how personalized is it? Static config that never changes uses cache: 'force-cache'. Content that changes every few minutes uses fetch() with revalidate. User-specific data uses the default no-store. Multiple components needing the same data add React cache() on top.

io.thecodeforge.nextjs.caching-decision.tsxTSX
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// ============================================
// Caching Decision Framework
// ============================================

/*
Data Type              | Changes     | Strategy
-----------------------|-------------|---------------------------
Site config            | Never       | fetch cache: 'force-cache'
Product catalog        | Hourly      | fetch next: { revalidate: 3600 }
Product prices         | Minutes     | fetch next: { revalidate: 300 }
User dashboard data    | Real-time   | fetch cache: 'no-store' (default)
Blog posts             | Daily       | Full Route Cache + revalidate: 86400
*/

import { Suspense } from 'react'
import { cache } from 'react'

export const getCategory = cache(async (slug: string) => {
  const res = await fetch(`https://api.example.com/categories/${slug}`, {
    next: { revalidate: 3600, tags: ['categories'] },
  })
  return res.json()
})

export default async function CategoryPage({ params }: { params: { category: string } }) {
  const category = await getCategory(params.category)

  return (
    <div>
      <h1>{category.name}</h1>
      <Suspense fallback={<ProductGridSkeleton />}>
        <ProductList category={params.category} />
      </Suspense>
    </div>
  )
}

async function ProductList({ category }: { category: string }) {
  const products = await fetch(`https://api.example.com/products?category=${category}`, {
    next: { revalidate: 300, tags: ['products'] },
  }).then(r => r.json())
  return <div>{products.map((p: any) => <div key={p.id}>{p.name}</div>)}</div>
}

function ProductGridSkeleton() {
  return (
    <div className="grid grid-cols-3 gap-4">
      {Array.from({ length: 6 }).map((_, i) => (
        <div key={i} className="h-40 animate-pulse rounded bg-muted" />
      ))}
    </div>
  )
}
Combine All Four Layers on Complex Pages
  • Full Route Cache for the static shell — opt in with cacheable fetches
  • PPR Suspense boundaries separate static from dynamic
  • fetch() with different revalidate values per data source
  • React cache() deduplicates shared data — category info fetched once
Production Insight
Complex pages use all four layers simultaneously — each data source has its own cache strategy.
Rule: start with default no-store for user data, add revalidate: N for content — optimize from there.
Key Takeaway
All four layers compose on a single page — static shell, dynamic holes, per-fetch freshness, deduplication.
Rule: do not pick one layer for everything — each data source needs its own cache strategy.

Client-Side Caching: Don't Bloat Your Server for Data the Browser Already Has

Most devs forget that caching isn't just a server concern. Every time your client re-fetches data it already holds, you burn bandwidth and CPU on both ends. That's amateur hour.

Next.js doesn't mandate a client cache — you build it. The weapon of choice is a stale-while-revalidate pattern with a simple state wrapper. SWR and TanStack Query dominate this space, but I've seen teams roll their own with 30 lines of code and zero dependencies.

The strategy: cache API responses in memory or localStorage with a timestamp. On subsequent mounts, return cached data immediately, then fire a background fetch to validate freshness. Your UI never blocks, the server gets fewer hammerings, and users see instant renders.

Where this bites you: optimistic UIs that show stale data without a refresh indicator. Always pair client caching with a subtle loading skeleton or a 'refreshing' badge. Otherwise, users edit a comment they already deleted — and that's a support ticket you don't want.

StaleWhileRevalidate.jsJAVASCRIPT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// io.thecodeforge — javascript tutorial

const cache = new Map();

async function fetchWithClientCache(url, ttlMs = 30000) {
  const now = Date.now();
  const cached = cache.get(url);

  if (cached && now - cached.timestamp < ttlMs) {
    // Fire background revalidation
    fetch(url)
      .then(r => r.json())
      .then(data => cache.set(url, { data, timestamp: Date.now() }))
      .catch(() => {}); // silent fail — stale is fine
    return cached.data;
  }

  const res = await fetch(url);
  const data = await res.json();
  cache.set(url, { data, timestamp: now });
  return data;
}

// Usage in component
const userOrders = await fetchWithClientCache('/api/orders', 60000);
Output
First call: fresh request to /api/orders.
Subsequent calls within 60s: cached data instantly, then background refresh.
After 60s: new fetch, old cache evicted.
Memory Leak Trap:
Your cache map will grow unbounded if you never evict. Set a max size (e.g., 50 entries) or use LRU eviction. Production apps that skip this leak 200MB+ in browser tab memory after 30 minutes of navigation.
Key Takeaway
Client cache is your first line of defense. Serve stale, revalidate in background, never block the UI.

API Caching: Why Your /api/products Endpoint Is Your Slowest Dependency

You've got a Next.js API route that queries a database and joins four tables. It takes 450ms cold. Multiply that by 10,000 requests per hour, and you're either paying for over-provisioned database connections or piling request latency.

The fix: HTTP caching at the route level. Next.js API routes don't automatically cache — you have to opt in. Use the Cache-Control response header to tell CDNs and browsers how long they can keep your response. For public, mostly-static data (e.g., product catalog, store hours), set s-maxage=3600, stale-while-revalidate=120.

stale-while-revalidate is your best friend here. It tells the CDN: serve the stale copy for up to 120 seconds while you revalidate in the background. Users never see a loading spinner, and your origin server gets fewer requests.

Where this breaks: authenticated endpoints. Never cache responses containing user-specific data at the CDN level. Use private, no-cache for those. Otherwise, Alice logs out and sees Bob's checkout page — GDPR violation speedrun.

APIRouteCache.jsJAVASCRIPT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// io.thecodeforge — javascript tutorial

export async function GET(request) {
  const products = await fetchProductsFromDB(); // 450ms cold

  return new Response(JSON.stringify(products), {
    status: 200,
    headers: {
      'Content-Type': 'application/json',
      'Cache-Control': 'public, s-maxage=3600, stale-while-revalidate=120'
    }
  });
}

// Vercel Edge or Node.js runtime — both respect Cache-Control
// For ISR-like behavior: pair with revalidate in next.config.js
Output
First request: 450ms.
Next 3599 seconds: instant (CDN cache hit).
Between 3600s and 3720s: serves stale, background revalidation.
After 3720s: fresh fetch (450ms again).
Senior Shortcut:
Add a ?fresh=true query param override. In your API route, check for it. If present, bypass cache and force a fresh fetch. This lets you manually purge cache during deploys without waiting for TTL.
Key Takeaway
API routes are not cached by default. Set Cache-Control with s-maxage + stale-while-revalidate to cut origin load by 90%.

How Next.js Optimizes Caching Out of the Box – And When to Shut It Off

Next.js isn't just a framework; it's a caching appliance that defaults to aggressive, build-time, and fetch-level caching. Your GET request to https://api.example.com/prices? By default, the response is cached in the Full Route Cache and the Data Cache simultaneously. The WHY is performance: serve static HTML from edge nodes, zero server cold starts. The HOW is automatic: any fetch() inside a static page gets its result cached indefinitely. This is great for your landing page, catastrophic for your pricing page if prices change every 30 seconds. You must understand these defaults before you can safely override them. Production teams waste days debugging stale data because they never read the docs on fetch options like cache: 'no-store' or next: { revalidate: 30 }. The framework is not magic; it's a set of sensible defaults that become dangerous when you assume they don't exist. Inspect the Cache-Control headers on your deployment immediately.

checkDefaults.jsJAVASCRIPT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// io.thecodeforge — javascript tutorial

// See what Next.js does to a simple fetch by default
async function getPricing() {
  // No options? Next.js caches this in the Data Cache.
  const res = await fetch('https://api.example.com/prices');
  return res.json();
}

// Force it to be fresh per request
async function getFreshPricing() {
  const res = await fetch('https://api.example.com/prices', {
    cache: 'no-store'
  });
  return res.json();
}

export default async function Page() {
  const prices = await getFreshPricing();
  return <div>{prices.amount}</div>;
}
Output
Fresh price every request. No stale data.
Production Trap:
If you see a fetch without explicit cache behavior, assume it's cached at build time until you prove otherwise.
Key Takeaway
Next.js caches everything by default. Explicitly opt out with 'cache: no-store' for dynamic data.

Key Takeaways and Best Practices – Cache Less Than You Think

Most caching failures happen because teams cache too much, too early. Before adding a cache, ask: does this request change rarely, or is it shared across many users without personalization? If neither, skip the cache. The cost of stale data and invalidation complexity often outweighs the performance gain.

A common pitfall is caching API responses at the page level, then breaking personalization or authentication. Instead, isolate caching to the specific data layer — cache database query results with a short TTL, not the entire rendered page. Use Next.js stale-while-revalidate for fresh background fetches without blocking the UI.

Always start with no cache. Profile actual latency. Only add caching when you can measure a real bottleneck. And when you do cache, always set an explicit time-to-live (TTL) — never allow unbounded caches in production. Prefer short TTLs (seconds, not hours) and rely on fast origin requests rather than serving stale content.

stale-while-revalidate.jsJAVASCRIPT
1
2
3
4
5
6
7
8
9
10
11
12
// next.js — static fetch with stale-while-revalidate
export async function getServerSideProps(context) {
  const { userId } = context.query;

  const res = await fetch(`https://api.example.com/user/${userId}`, {
    headers: { 'Cache-Control': 's-maxage=10, stale-while-revalidate=60' },
  });

  const user = await res.json();

  return { props: { user } };
}
Output
// On first request, data cached for 10 seconds.
// Subsequent requests within 60 seconds get stale data while server re-fetches in background.
Production Trap:
Never cache authenticated or session-specific data at the page level. Always scope caching to the public data layer. One stale auth response can leak session data.
Key Takeaway
Cache only when TTL-bound, measurable, and data-safe — otherwise, let the origin handle it.

The `use cache` Directive (Next.js 15+)

Next.js 15 introduces use cache as the declarative way to control caching behavior at the component and function level. Unlike the older unstable_cache, use cache is stable, type-safe, and integrates directly with React's server components. You place it at the top of an async function or component to mark it for caching. The directive tells Next.js to cache the result after first execution, then serve the cached version for subsequent requests within the cache duration. Combine it with cacheLife to set precise expiration times. This replaces manual cache key management and reduces boilerplate. Use use cache when you need per-function caching without polluting your data layer. It's especially powerful for database queries and API calls that return identical results across requests. The main downside: you lose fine-grained control over cache invalidation that unstable_cache offered via tags. But for most use cases, the simplicity wins.

useCacheExample.jsJAVASCRIPT
1
2
3
4
5
6
7
8
9
10
11
// io.thecodeforge — javascript tutorial

import { cacheLife } from 'next/cache'

export default async function ProductGrid() {
  'use cache'
  cacheLife('hours', 1)

  const products = await db.query('SELECT * FROM products LIMIT 20')
  return <ProductList items={products} />
}
Output
Caches the component output for 1 hour. Subsequent requests return cached HTML without re-executing the query.
Production Trap:
use cache only works in Server Components. Placing it in a Client Component throws a build error. Always verify your file extension matches .tsx or .jsx with the 'use server' directive absent.
Key Takeaway
Use use cache for component-level caching — it replaces manual caching wrappers with a single directive.

A Practical Mental Model for Next.js Caching

Stop thinking of Next.js caching as a multi-layer puzzle. Instead, see it as a hierarchy of time-to-live decisions. At the top: Full Route Cache caches entire pages at build time — great for blogs, terrible for inventory. Below that: fetch() cache controls individual network requests per route segment. At the bottom: React cache() deduplicates identical requests within a single render pass. The key rule: every layer is optional and independent. Disable layers you don't need using revalidate, dynamic, or no-store. For dynamic apps like dashboards, set dynamic = 'force-dynamic' on the page and cache specific data fetching functions with use cache. For static sites, let Full Route Cache work and only opt out for user-specific content. Memory model: think of caching as a stack of CDs. Full Route Cache is the album cover — fast but outdated. fetch() cache is a song you skip. React cache() is the needle reading the track twice, then remembering.

cacheModel.jsJAVASCRIPT
1
2
3
4
5
6
7
8
9
10
// io.thecodeforge — javascript tutorial

export const dynamic = 'force-dynamic'

export default async function Dashboard({ userId }) {
  const data = await fetch('https://api.example.com/user/' + userId, {
    cache: 'no-store'
  })
  return <UserProfile data={await data.json()} />
}
Output
Forces dynamic rendering, skips Full Route Cache, bypasses fetch cache — fresh data on every request.
Production Trap:
Overriding caching on a parent layout cascades to all child pages. A single dynamic = 'force-dynamic' on a layout makes every nested route dynamic — killing Full Route Cache globally.
Key Takeaway
Treat caching as a per-layer TTL stack. Disable aggressively, enable only where fresh data is not required.
● Production incidentPOST-MORTEMseverity: high

Product prices stale for 24 hours due to Full Route Cache

Symptom
Customer support tickets about incorrect prices started 2 hours after a CMS price update. The prices were correct in the CMS but wrong on the frontend. A redeployment fixed the issue temporarily.
Assumption
The team assumed fetch() would stay fresh by default in Next.js 16, but they had explicitly opted into permanent caching with cache: 'force-cache' for performance.
Root cause
The product page used fetch(https://api.example.com/products/${slug}, { cache: 'force-cache' }). This opts the data into the Data Cache permanently, and combined with generateStaticParams, the page was fully static at build time. The result was baked into the Full Route Cache. Without revalidate or on-demand invalidation, the cached HTML persisted until the next deploy.
Fix
Removed cache: 'force-cache' and added next: { revalidate: 300 } to the fetch() call — this enables stale-while-revalidate every 5 minutes. For prices that change frequently, added on-demand revalidation via revalidateTag('products') triggered by a CMS webhook.
Key lesson
  • In Next.js 16 fetch() defaults to no-store — you must opt-in to caching with revalidate or cache: 'force-cache'
  • Full Route Cache bakes cached fetch results into static HTML — stale data persists without revalidation
  • CMS-driven content needs webhook-triggered revalidation — do not rely on time-based revalidation alone
Production debug guideDiagnose stale data, slow pages, and cache invalidation issues6 entries
Symptom · 01
Page shows stale data after CMS update
Fix
Check if the page is in the Full Route Cache — look for x-nextjs-cache: HIT or STALE. Add next: { revalidate } to fetch() or trigger on-demand revalidation via revalidatePath().
Symptom · 02
Page is slow despite using static generation
Fix
Check if a dynamic fetch() (no-store) or cookies() call is forcing the segment to stream — with PPR only that Suspense boundary streams, not the whole page.
Symptom · 03
Same data fetched multiple times in one request
Fix
Wrap the fetch call with React cache() — it deduplicates identical calls within a single render pass.
Symptom · 04
Partial Prerendering not working — entire page streams
Fix
Verify the static shell has no uncached data fetches at the root. Dynamic content must be wrapped in Suspense and use cache: 'no-store'.
Symptom · 05
fetch() cache not invalidating after revalidateTag() call
Fix
Verify the tag on the fetch call matches the tag passed to revalidateTag(). Tags are case-sensitive and must match exactly.
Symptom · 06
Build takes too long due to excessive static generation
Fix
Check how many pages are being statically generated. Use dynamicParams: true to generate on-demand, or remove cache: 'force-cache' to skip Full Route Cache.
★ Next.js Caching Quick Debug ReferenceFast commands for diagnosing Next.js caching issues in production
Stale data on a specific page
Immediate action
Check the response cache header
Commands
curl -I https://your-site.com/products/123 | grep -i 'x-nextjs-cache'
curl -s -D - https://your-site.com/products/123 -o /dev/null | grep x-nextjs-cache
Fix now
If HIT or STALE, the page is cached — add next: { revalidate: 60 } to the fetch() or call revalidatePath() from a webhook
Build generates too many static pages+
Immediate action
Check which routes are being statically generated
Commands
next build 2>&1 | grep '○\|●\|ƒ'
cat .next/routes-manifest.json | jq '.staticRoutes | length'
Fix now
○ = static, ● = SSG, ƒ = dynamic. Remove cache: 'force-cache' or add export const dynamic = 'force-dynamic'
React cache() not deduplicating fetches+
Immediate action
Verify the fetch call is wrapped at module level, not inside the component
Commands
grep -rn 'cache(' src/lib/ --include='*.ts' | head -10
cat src/app/page.tsx | grep -B 2 -A 5 'cache('
Fix now
Move the cached function to a separate lib/data.ts file and wrap with cache() at the top level
On-demand revalidation not working+
Immediate action
Check the revalidation route handler
Commands
cat src/app/api/revalidate/route.ts
curl -X POST https://your-site.com/api/revalidate -H 'Authorization: Bearer TOKEN' -d '{"tag":"products"}'
Fix now
Verify the secret matches, revalidatePath or revalidateTag is called correctly, and the response returns { revalidated: true }
Next.js 16 Caching Layers Compared
LayerWhen It OperatesWhat It CachesInvalidationDefault Behavior
Full Route CacheBuild timeEntire HTML + RSC payloadRedeploy, revalidate: N, revalidatePath/TagOnly if all data is cacheable
Partial PrerenderingBuild + requestStatic shell at build, dynamic holes at requestShell: same as Full Route. Holes: per-fetchEnabled by default
fetch() / Data CacheRequest timeIndividual fetch responsesrevalidate: N, revalidateTagno-store (fresh every request)
React cache()Single renderFunction return values within one requestCleared after renderNo caching — must wrap explicitly
Router Cache (client)BrowserRSC payloads for navigationrouter.refresh(), 30s/5min TTL30s static, 5min dynamic

Key takeaways

1
Next.js 16 has four server caching layers plus a client Router Cache
2
fetch() defaults to no-store in Next.js 15+
you must opt-in to caching with revalidate or cache: 'force-cache'
3
Partial Prerendering splits pages into static shells and dynamic streaming holes
dynamic APIs only affect their boundary
4
React cache() deduplicates within one render pass
define at module level, not inside components
5
Each data source needs its own cache strategy
do not apply one setting to all fetches

Common mistakes to avoid

5 patterns
×

Opting into permanent caching without revalidation

Symptom
CMS updates are invisible on the frontend. Pages show data from the last build.
Fix
Use next: { revalidate: N } instead of cache: 'force-cache', or add on-demand revalidation with revalidateTag.
×

Assuming fetch() caches by default

Symptom
Pages are slow — every request fetches fresh data because no caching was configured.
Fix
In Next.js 16, add next: { revalidate } to opt into caching. The default is no-store.
×

Using dynamic APIs outside Suspense boundaries

Symptom
Static shell is delayed — the whole segment waits for dynamic data.
Fix
Move cookies(), headers(), and searchParams usage into Suspense-wrapped components.
×

Defining React cache() inside a component

Symptom
Data is fetched multiple times during one render.
Fix
Move the cached function to lib/data.ts and wrap with cache() at the module level.
×

Using fetch() caching in Client Components

Symptom
fetch() calls bypass Next.js caching entirely.
Fix
Fetch data in a Server Component and pass as props to the Client Component.
INTERVIEW PREP · PRACTICE MODE

Interview Questions on This Topic

Q01SENIOR
What are the four server caching layers in Next.js 16, and when does eac...
Q02SENIOR
What is the difference between React cache() and fetch() caching?
Q03SENIOR
How would you debug a page that shows stale data after a CMS update?
Q01 of 03SENIOR

What are the four server caching layers in Next.js 16, and when does each operate?

ANSWER
Full Route Cache operates at build time — stores entire HTML when all data is cacheable. Partial Prerendering operates at build + request — static shell cached, dynamic holes stream. Data Cache (fetch) operates at request time — each fetch defaults to no-store unless you opt in with revalidate. React cache() operates within a single render — deduplicates calls but doesn't persist.
FAQ · 3 QUESTIONS

Frequently Asked Questions

01
How do I invalidate the cache when my CMS updates content?
02
Can I use fetch() caching in Client Components?
03
What is the performance difference between Full Route Cache and SSR?
🔥

That's React.js. Mark it forged?

9 min read · try the examples if you haven't

Previous
How to Build an AI Agent with Next.js, LangChain & Supabase
29 / 47 · React.js
Next
How to Build a Design System with shadcn/ui, Tailwind & Radix