๐ Next.js Revalidating (Easy Notes)
๐ 1. Revalidation kya hota hai?
๐ Simple:
Cache ko update karna
๐ Why needed?
Fast response bhi mile
Latest data bhi mile
๐ Example:
Blog post update โ new data show hona chahiye
โก 2. Types of Revalidation
๐ข 1. Time-based Revalidation
๐ Automatic update after time
import { cacheLife } from "next/cache"
export async function getData() {
"use cache"
cacheLife("hours")
}๐ 1 hour baad auto refresh
๐ Defined using cacheLife
๐ต 2. On-Demand Revalidation
๐ Manual update (best ๐ฅ)
๐งฉ 3. cacheTag (Tagging Data)
import { cacheTag } from "next/cache"
export async function getProducts() {
"use cache"
cacheTag("products")
}๐ Data ko tag diya
๐ 4. revalidateTag (Background Update)
import { revalidateTag } from "next/cache"
export async function updateData() {
// DB update
revalidateTag("products")
}๐ Old data show hoga
๐ New data background me load hoga
๐ stale-while-revalidate concept
โก 5. updateTag (Instant Update)
import { updateTag } from "next/cache"
export async function createPost() {
// DB save
updateTag("posts")
}๐ Immediate update
๐ User ko instantly new data dikhega
๐ฅ Difference
FeatureupdateTagrevalidateTagUpdateInstantBackgroundUXLatest dataSlight delayUse caseCreate/update formBlog/products
๐ฃ๏ธ 6. revalidatePath (Route Refresh)
import { revalidatePath } from "next/cache"
export async function updateUser() {
revalidatePath("/profile")
}๐ Entire page refresh
๐ Use when tag pata nahi ho
๐ง 7. cacheLife Advanced
cacheLife({
stale: 3600,
revalidate: 7200,
expire: 86400
})๐ Full control
๐ฏ 8. Kab kya use kare?
๐ Simple rule:
Static data โ cache + cacheLife
CMS / blog โ cacheTag + revalidateTag
Form submit โ updateTag
Page refresh โ revalidatePath
๐ง 9. Important Rules (Interview)
Revalidation = cache update
cacheLife = time-based
cacheTag = group data
revalidateTag = background update
updateTag = instant update
revalidatePath = page refresh
๐ฅ Short Revision (1 min)
๐ Cache fast hai
๐ Revalidation fresh data deta hai
๐ updateTag = instant
๐ revalidateTag = background
๐ฅ Super Simple Line
๐ "Next.js me caching fast hai aur revalidation usko fresh banata hai"
๐ Full Flow เคธเคฎเคเฅ
Data cache hua
User update karta hai
Server Action run
Cache revalidate/update
UI fresh data show