Server components run on the server to prepare content before sending it to the browser. This helps make pages faster and lighter.
Server component execution model in NextJS
export default async function MyServerComponent() { // Server-only code here const data = await fetchDataFromAPI(); return ( <div>{data.message}</div> ); }
Server components are React components that run only on the server.
They can fetch data directly and do not include client-side JavaScript.
export default function Greeting() { const time = new Date().toLocaleTimeString(); return <p>Current time: {time}</p>; }
export default async function UserProfile() { const res = await fetch('https://api.example.com/user'); const user = await res.json(); return <h1>Welcome, {user.name}!</h1>; }
This server component shows the current server time when the page loads. It runs only on the server and sends the rendered HTML to the browser.
export default async function ServerTime() { const now = new Date().toLocaleString(); return ( <main> <h1>Server Time</h1> <p>The current server time is: {now}</p> </main> ); }
Server components cannot use browser-only APIs like window or document.
They help reduce JavaScript sent to the client, improving performance.
Use server components for static or data-fetching parts of your UI.
Server components run only on the server and prepare HTML before sending it to the browser.
They improve performance by reducing client-side JavaScript.
Use them to fetch data securely and render static content.
Practice
Solution
Step 1: Understand server component role
Server components run only on the server and generate HTML before sending it to the browser.Step 2: Compare with client components
Client components handle interactions and state, but server components focus on data fetching and static rendering.Final Answer:
To run code only on the server and send pre-rendered HTML to the client -> Option CQuick Check:
Server components = server-only rendering [OK]
- Confusing server components with client components
- Thinking server components handle user events
- Assuming server components run in the browser
Solution
Step 1: Identify server component syntax
Server components can be async functions that fetch data and return JSX.Step 2: Check options for server component traits
export default async function MyComponent() { return <div>Hello from server</div> } exports an async function returning JSX, which is valid for server components.Final Answer:
export default async function MyComponent() { return <div>Hello from server</div> } -> Option AQuick Check:
Async function export = server component [OK]
- Assuming all exported functions run on client
- Confusing runtime or dynamic exports with server component definition
- Not using async when fetching data in server components
export default async function Page() {
const data = await fetch('https://api.example.com/data').then(res => res.json())
return <div>Count: {data.count}</div>
}Solution
Step 1: Understand server component data fetching
Server components can fetch data using await and render the result directly.Step 2: Predict output with given API response
If API returns { count: 42 }, the component renders <div>Count: 42</div> as static HTML.Final Answer:
<div>Count: 42</div> (assuming API returns { count: 42 }) -> Option DQuick Check:
Server fetch + await = pre-rendered data [OK]
- Thinking fetch is disallowed in server components
- Expecting client-side loading states in server components
- Assuming data is undefined without awaiting fetch
export default function Page() {
const data = fetch('/api/data').then(res => res.json())
return <div>Data: {data.value}</div>
}Solution
Step 1: Check fetch usage in server component
fetch returns a Promise; without await, data is a Promise object.Step 2: Understand JSX rendering with Promise
JSX cannot render a Promise directly, causing incorrect output or error.Final Answer:
Missing async/await causes data to be a Promise, not resolved value -> Option AQuick Check:
Always await fetch in server components [OK]
- Forgetting async keyword on function
- Trying to render Promise instead of data
- Assuming fetch is disallowed in server components
Solution
Step 1: Understand server and client component roles
Server components fetch data securely and prepare HTML; client components handle user events.Step 2: Combine by passing data as props
Server component fetches data, then passes it to client component which manages interactions.Final Answer:
Use a server component to fetch data and pass it as props to a client component for interaction -> Option BQuick Check:
Server fetch + client interaction = Use a server component to fetch data and pass it as props to a client component for interaction [OK]
- Fetching data in client component only
- Trying to handle user events in server components
- Passing data from client to server component via props
Ready to prove it?
Pass the test and this pattern moves unknown
