Fetching in a Server Component
Because a Server Component runs on the server, it can be an async function and
await data directly. There is no loading state to wire up and no useEffect:
the component simply suspends until its data is ready.
// app/blog/page.tsx
export default async function Page() {
const data = await fetch('https://api.example.com/blog');
const posts = await data.json();
return (
<ul>
{posts.map((post) => (
<li key={post.id}>{post.title}</li>
))}
</ul>
);
}
You can also reach a database or ORM directly, since that code never ships to the browser and can use secrets safely.
Two facts shape how this behaves. First, identical fetch requests in one
render pass are memoised by React, so the same URL fetched in two components
runs once. Second, a fetch is not cached by default: it blocks the page
from rendering until it resolves. To cache a result, use the use cache
directive; to avoid blocking, wrap the fetching component in <Suspense> so the
rest of the page streams immediately.
The rule of thumb: fetch in the component that needs the data, close to the server-side source, and let the framework handle memoisation.
You are about to answer a short quiz on async Server Component + fetch.