๐ฏ 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}
| Style | How | Good for |
|---|---|---|
| Static | default when nothing is dynamic | about pages, docs, blog posts |
| Revalidated (ISR) | export const revalidate = 300 | product lists, news |
| Dynamic | cache: "no-store", cookies, headers | dashboards, 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 = 60refreshes 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.