0
0
NextJSframework~10 mins

What can run in server components in NextJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - What can run in server components
Start Server Component
Run Server-only Code
Fetch Data from DB or API
Render JSX to HTML
Send HTML to Client
Client Hydrates Interactive Parts
End
Server components run only on the server, fetching data and rendering HTML before sending it to the client.
Execution Sample
NextJS
export default async function Page() {
  const data = await fetch('https://api.example.com/data');
  const json = await data.json();
  return <div>{json.message}</div>;
}
This server component fetches data from an API and renders it as HTML on the server.
Execution Table
StepActionCode ExecutedResult
1Start Server ComponentPage() function calledComponent starts on server
2Fetch Dataawait fetch('https://api.example.com/data')Data fetched from API
3Parse JSONawait data.json()JSON object received
4Render JSXreturn <div>{json.message}</div>HTML string created with message
5Send HTMLServer sends HTML to clientClient receives rendered HTML
6Client HydrationClient hydrates interactive partsPage becomes interactive
7EndComponent rendering completeNo client-side code ran during render
💡 Server component finishes after rendering HTML and sending it to client; no client JS runs during server render.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
dataundefinedResponse object from fetchResponse objectResponse object
jsonundefinedundefinedParsed JSON objectParsed JSON object
Key Moments - 3 Insights
Can server components run browser-only code like accessing window or document?
No, server components run only on the server where browser objects like window or document do not exist. See execution_table step 7 where no client JS runs during server render.
Can server components fetch data directly from a database?
Yes, server components can run server-only code like database queries or API calls as shown in execution_table steps 2 and 3.
Does server component code run on the client?
No, server component code runs only on the server. The client receives only the rendered HTML and hydrates interactive parts separately (execution_table step 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is data fetched from the API?
AStep 4
BStep 2
CStep 6
DStep 1
💡 Hint
Check the 'Action' column for 'Fetch Data' in the execution_table.
According to variable_tracker, what is the value of 'json' after step 3?
AParsed JSON object
BResponse object
Cundefined
Dnull
💡 Hint
Look at the 'json' row and the 'After Step 3' column in variable_tracker.
If you try to use 'window' inside this server component, what happens?
AIt works normally on the server
BIt runs only after client hydration
CIt causes an error because 'window' is not defined on the server
DIt delays the server render
💡 Hint
Refer to key_moments about browser-only code in server components.
Concept Snapshot
Server Components in Next.js run only on the server.
They can fetch data from APIs or databases directly.
They render JSX to HTML before sending to the client.
No browser-only code (like window or document) can run here.
Client receives static HTML and hydrates interactive parts separately.
Full Transcript
Server components in Next.js run exclusively on the server. When a server component is called, it can perform server-only tasks such as fetching data from APIs or databases. The component then renders JSX into HTML on the server. This HTML is sent to the client, which then hydrates any interactive parts. Server components cannot use browser-specific objects like window or document because these do not exist on the server. The execution table shows the step-by-step process from starting the component, fetching data, rendering HTML, sending it to the client, and client hydration. The variable tracker shows how variables like the fetched data and parsed JSON change during execution. Key moments clarify common confusions about what can and cannot run in server components. The visual quiz tests understanding of these steps and concepts.