LESSON 01 / 04

Fetching Data & Caching

Fetch data directly inside server components with async/await, and decide how fresh it must be: cached forever, revalidated every few minutes, or fresh on every request.

๐ŸŽ“Mediumlevel
โฑ๏ธ35 minto finish
โœ๏ธ1activities

๐Ÿ“‚ Data & Routing๐Ÿท๏ธ Next.js

๐ŸŽฏ By the end of this lesson you canโ€ฆ

  • Fetch data in a server component
  • Choose static, revalidated or dynamic data
  • Fetch in parallel
โš›๏ธ JSX app/weather/page.jsx ๐Ÿ‘† Tap a line to explain it
1export const revalidate = 600;2 3export default async function WeatherPage() {4  const res = await fetch("https://api.example.com/weather?city=Pune");5  if (!res.ok) throw new Error("Weather service is down");6  const w = await res.json();7  return <h1>{w.city}: {w.temp}ยฐC {w.sky}</h1>;8}
โš›๏ธ JSX app/dashboard/page.jsx ๐Ÿ‘† Tap a line to explain it
1async function getUser() { /* โ€ฆ */ }2async function getNotes() { /* โ€ฆ */ }3 4export default async function Dashboard() {5  const [user, notes] = await Promise.all([getUser(), getNotes()]);6  return <h1>{user.name} has {notes.length} notes</h1>;7}
Freshness options
StyleHowGood for
Staticdefault when nothing is dynamicabout pages, docs, blog posts
Revalidated (ISR)export const revalidate = 300product lists, news
Dynamiccache: "no-store", cookies, headersdashboards, carts, personal pages
๐Ÿ’ก Concept check
A product catalogue changes a few times a day. Best choice?

๐Ÿ“Œ Key takeaways

  • const data = await fetch(url).then(r => r.json()) right inside the component.
  • export const revalidate = 60 refreshes cached data at most once a minute.
  • { cache: "no-store" } or reading cookies makes a page dynamic (fresh every request).

Finished reading & practising?

Saved in this browser. Create a free account to keep it forever.