0
0
NextJSframework~10 mins

Data fetching in server components in NextJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Data fetching in server components
Start Server Component Render
Call async data fetching function
Wait for data to be fetched
Receive data
Render component with data
Send HTML to client
End
The server component starts rendering, fetches data asynchronously, waits for it, then renders the UI with the data before sending HTML to the client.
Execution Sample
NextJS
export default async function Page() {
  const res = await fetch('https://api.example.com/data');
  const data = await res.json();
  return <div>{data.message}</div>;
}
This server component fetches data from an API, waits for the response, then renders a div showing the message from the data.
Execution Table
StepActionEvaluationResult
1Start rendering Page componentNo data yetComponent begins execution
2Call fetch('https://api.example.com/data')Starts network requestPromise pending
3Await fetch responseWait for networkResponse received
4Call res.json()Parse JSON bodyPromise pending
5Await res.json()Wait for parsing{"message":"Hello from API"}
6Render <div>{data.message}</div>Insert message into JSX<div>Hello from API</div>
7Send rendered HTML to clientHTML readyClient receives <div>Hello from API</div>
8EndRendering completeServer component finished
💡 Rendering ends after data is fetched and HTML is generated for the client.
Variable Tracker
VariableStartAfter Step 3After Step 5Final
resundefinedResponse objectResponse objectResponse object
dataundefinedundefined{"message":"Hello from API"}{"message":"Hello from API"}
Key Moments - 2 Insights
Why do we use 'await' before fetch and res.json()?
Because fetch and res.json() return promises that take time to complete. Using 'await' pauses execution until the data is ready, as shown in steps 3 and 5 of the execution table.
What happens if we try to render before data is fetched?
The component would not have the data to display, causing errors or empty content. The execution table shows rendering happens only after data is fetched (step 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'data' after step 3?
AResponse object
Bundefined
C{"message":"Hello from API"}
DPromise pending
💡 Hint
Check the 'data' variable in variable_tracker after step 3.
At which step does the component start rendering the HTML with the fetched data?
AStep 6
BStep 4
CStep 2
DStep 8
💡 Hint
Look at the 'Render
{data.message}
' action in the execution_table.
If the fetch call took longer, which step would be delayed?
AStep 1
BStep 6
CStep 3
DStep 8
💡 Hint
Step 3 is where the component waits for the fetch response.
Concept Snapshot
Data fetching in server components:
- Use async functions with await
- Fetch data before rendering
- Render UI with fetched data
- Server sends ready HTML to client
- No client-side loading needed
Full Transcript
In Next.js server components, data fetching happens during the server-side rendering process. The component function is async and uses await to pause execution until the fetch call completes and the JSON data is parsed. Once data is ready, the component renders JSX with the data included. The server then sends the fully rendered HTML to the client. This means the client receives content immediately without waiting for data loading. The execution table shows each step from starting the component, fetching data, parsing it, rendering, and sending HTML. Variables like 'res' and 'data' change as the fetch and parsing complete. Using await ensures the component waits for data before rendering, preventing errors or empty content.