Lesson 2.1

Fetching data in Server Components

A Server Component can be an async function, so fetching is just awaiting inside the component that needs the data.

4mIntermediate15.3k students

Overview

The component is the data layer

A Server Component can be an async function, so fetching is just awaiting inside the component that needs the data. There is no separate loader, no client-side effect, and no serialisation step you have to write yourself.

Because the code never reaches the browser, the component can hold a database connection or an API token directly. That is the single biggest structural advantage over fetching in a client effect.

The trap is sequencing. Awaiting one request and then another inside the same component creates a waterfall even when the two are unrelated. Start both promises first and await them together when neither depends on the other.

In this lesson you will:

  • Fetch directly in an async Server Component
  • Keep credentials off the client
  • Avoid the request waterfall

Resources

Previous Lesson
Next Lesson
Fetching data in Server Components — Next.js for Production — Vertex