Data Fetching
Data Fetching
💡 Why Should You Learn This?
Concepts
The advanced caching and ISR (Incremental Static Regeneration) features available in Next.js 14+'s App Router are essential skills for modern web applications. They directly impact high-traffic handling and user experience optimization, and are evaluated as key performance metrics at major companies like Naver, Coupang, and Toss.
Why Does It Matter?
If you fetch frequently changing data — such as a commerce product listing — from the server on every request, response times can exceed 3 seconds, increasing bounce rates by 50%. For content that needs periodic updates but not real-time delivery, such as news sites or blogs, a well-chosen revalidate strategy can cut CDN costs by 70%.
Core Concept
Next.js data fetching works like a "smart refrigerator." Just as a fridge checks expiration dates and only replaces items that have gone bad, Next.js manages data "freshness" and only re-fetches stale data from the server. The fetch cache acts as the refrigerator that stores data, while revalidate acts as the sensor that checks expiration.
Key Points
- ▸
fetch()caches data in two stages by default: Request Memoization and Data Cache - ▸The
revalidateoption controls how often data freshness is checked (in seconds) - ▸In App Router, combining
generateStaticParamswith revalidate enables optimization of dynamic routing as well
💡 ⚠️ Common Mistakes
- ▸Setting the
revalidateinterval too short, negating the benefits of caching (values under 5 seconds are almost never necessary) - ▸Not using cache tags, forcing you to invalidate the entire cache inefficiently (overusing
revalidatePath) - ▸Not combining
generateStaticParamswithrevalidate, missing out on dynamic routing optimization
💡 🎯 Interview Prep
Q: Explain the difference between ISR and SSG in Next.js, and when you would use each.
Q: How would you design a Next.js caching strategy for high-traffic scenarios?
Q: Describe the difference between revalidateTag and revalidatePath and give usage scenarios for each.
Hint: Describe ISR as a hybrid approach that allows data updates even after the build, and support your answer with real-world examples (e.g., commerce product listings). Mentioning the differences between caching levels (Request Memoization vs. Data Cache) and the advantages of tag-based invalidation will earn you high marks.