Tutorials/NextJS/Getting Started
Lesson

Revalidating

Getting Started/NextJS

๐Ÿš€ 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

plaintext
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)

plaintext
import { cacheTag } from "next/cache"

export async function getProducts() {
  "use cache"
  cacheTag("products")
}

๐Ÿ‘‰ Data ko tag diya


๐Ÿ”„ 4. revalidateTag (Background Update)

plaintext
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)

plaintext
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)

plaintext
import { revalidatePath } from "next/cache"

export async function updateUser() {
  revalidatePath("/profile")
}

๐Ÿ‘‰ Entire page refresh

๐Ÿ“Œ Use when tag pata nahi ho


๐Ÿง  7. cacheLife Advanced

plaintext
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 เคธเคฎเคเฅ‹

  1. Data cache hua

  2. User update karta hai

  3. Server Action run

  4. Cache revalidate/update

  5. UI fresh data show

Revalidating | NextJS | Softcrayons Tech Solutions