π Next.js 16 Rendering, Caching & Revalidation Deep Dive
Overview
Next.js App Router mein rendering aur data fetching alag systems nahi hain.
Rendering engine, caching layer, fetch system aur revalidation mechanism ek saath milkar decide karte hain:
- Page static hoga ya dynamic
- Data cache hoga ya nahi
- Cached data kab update hoga
- User ko content kaise serve kiya jayega
- Streaming use hogi ya nahi
Is chapter ka goal ye samajhna hai ki Next.js request receive karne ke baad internally kaise decisions leta hai.
The Rendering Decision Engine
Next.js mein rendering manually select nahi hoti.
Most cases mein framework aapke code ko analyze karke rendering strategy decide karta hai.
Major Rendering Modes
| Mode | Description |
|---|---|
| Static Rendering | HTML pehle se generate aur cache hota hai |
| Dynamic Rendering | Har request par page render hota hai |
| Streaming | Page ko chunks mein bheja jata hai |
1. Static Rendering
Static rendering mein HTML pehle generate ho jata hai.
User request aane par server ko page dubara render nahi karna padta.
export default async function Page() {
const res = await fetch("https://api.example.com/posts");
const posts = await res.json();
return <Posts posts={posts} />;
}Characteristics
- Fastest response time
- Excellent SEO
- Minimal server work
- Cache friendly
Mental Model
Restaurant mein ready-made food counter par rakha hua hai.
Customer aaya β food turant serve.
2. Dynamic Rendering
Dynamic rendering mein page har request par generate hota hai.
await fetch(url, {
cache: "no-store",
});Characteristics
- Fresh data
- Request specific content
- Higher server cost
- Slower than static rendering
Use Cases
- User dashboards
- Banking apps
- Admin panels
- Personalized content
Mental Model
Customer order deta hai.
Kitchen us moment par fresh food prepare karti hai.
3. Streaming
Traditional SSR mein poora page ready hone ka wait karna padta tha.
Streaming mein page parts mein browser ko bheja jata hai.
Traditional Flow
Data Fetch
β
Full Render
β
Send HTMLStreaming Flow
Header Ready
β
Send Immediately
β
Slow Data Loading
β
Send Remaining Parts LaterBenefit
User ko page fast feel hota hai.
Actual total render time same bhi ho sakta hai, lekin perceived performance improve ho jati hai.
Analogy
Restaurant pehle starter serve karta hai.
Main course baad mein aata hai.
How Next.js Decides Static vs Dynamic
Next.js aapka code analyze karta hai.
Agar dynamic signals nahi milte to page static render ho sakta hai.
Static-Friendly Example
const res = await fetch(url);Default behaviour:
cache: "force-cache";Result:
- Cache enabled
- Static rendering possible
What Breaks Static Rendering?
1. no-store
await fetch(url, {
cache: "no-store",
});Har request par fresh data.
Static rendering impossible.
2. Request-Specific APIs
cookies();
headers();Ye values har request mein different ho sakti hain.
Isliye page dynamic render hota hai.
3. Request Dependent Logic
Date.now();Math.random();Request ke according output change ho sakta hai.
Static output reliable nahi rahega.
4. Explicit Dynamic Rendering
export const dynamic = "force-dynamic";Next.js ko directly instruction mil jati hai:
Always render dynamically.
Caching Fundamentals
Caching ka basic purpose:
Expensive work ko repeat karne se bachana.
Without Cache
Request
β
API
β
ResponseHar request par same work repeat.
With Cache
Request
β
Cache Check
β
ResponseNetwork request avoid ho sakti hai.
Default Cache Behaviour
await fetch(url);Practical mental model:
await fetch(url, {
cache: "force-cache",
});Flow
First Request:
Request
β
API
β
Store CacheFuture Requests:
Request
β
Cache Hit
β
ResponseCache Modes
force-cache
await fetch(url, {
cache: "force-cache",
});Best For
- Blog posts
- Documentation
- Marketing pages
- Static content
Characteristics
- Maximum performance
- Cached response reuse
no-store
await fetch(url, {
cache: "no-store",
});Best For
- Dashboards
- Live data
- User-specific content
Characteristics
- No cache
- Always fresh
Revalidation (Hybrid Strategy)
Pure static aur pure dynamic ke beech ka solution.
await fetch(url, {
next: {
revalidate: 60,
},
});Behaviour
Cache Response
β
60 Seconds Valid
β
Refresh CacheBenefit
- Fast responses
- Periodic updates
- Lower server load
Use Cases
- News websites
- Product inventory
- Public APIs
Request Deduplication
Ek render cycle mein multiple identical requests ko Next.js merge kar deta hai.
const a = await fetch("/api/posts");
const b = await fetch("/api/posts");Actual Behaviour
2 fetch calls
β
1 network request
β
Shared responseRequirements
Deduplication tabhi hoti hai jab:
- URL same ho
- Fetch options same ho
Benefit
- Reduced network traffic
- Faster rendering
- Better scalability
Tag-Based Cache Invalidation
Time-based revalidation har situation ke liye ideal nahi hoti.
Production applications aksar tag-based invalidation use karti hain.
Assigning Tags
await fetch("/api/posts", {
next: {
tags: ["posts"],
},
});Invalidating Tags
import { revalidateTag } from "next/cache";
await revalidateTag("posts");Result
Jitne bhi cached fetches "posts" tag use kar rahe the:
posts
β
Invalidated
β
Fresh Data Next RequestReal Production Example
Blog System:
/blog
/blog/[slug]Dono same content source use karte hain.
next: {
tags: ["posts"];
}Post update hone par:
revalidateTag("posts");Result:
- Blog listing refresh
- Individual article refresh
- Cache manually clear karne ki zarurat nahi
Rendering + Caching Relationship
| Configuration | Rendering |
|---|---|
| force-cache | Usually Static |
| revalidate | Hybrid |
| no-store | Dynamic |
| cookies() | Dynamic |
| headers() | Dynamic |
| force-dynamic | Dynamic |
Common Problems
Data Update Nahi Ho Raha
Possible Causes:
- force-cache active
- cache invalidation missing
Solutions
cache: "no-store";ya
next: {
revalidate: 60;
}API Hit Nahi Ho Rahi
Possible Cause:
Request deduplication.
Multiple fetch calls same response share kar rahe hain.
Old Data Dikh Raha Hai
Possible Causes:
- Cache expired nahi hua
- Wrong tag invalidate hua
- Revalidation configured nahi hai
Page Slow Lag Raha Hai
Possible Solutions:
- Streaming
- Suspense
- Better cache strategy
Final Mental Model
Next.js request receive karne ke baad roughly ye process follow karta hai:
Request
β
Analyze Code
β
Check Cache Rules
β
Check Dynamic Signals
β
Choose Rendering Strategy
β
Fetch Data
β
Apply Cache
β
Stream Response
β
Send HTMLGolden Rules
- Static = Maximum Performance
- Dynamic = Maximum Freshness
- Revalidation = Balance
- Tags = Fine-Grained Control
- Streaming = Better User Experience
Rendering, Caching aur Revalidation alag systems nahi hain.
Ye Next.js App Router ke ek hi rendering engine ke interconnected parts hain.