Data fetching in server components lets your app get data before showing the page. This makes pages load faster and work better for users.
Data fetching in server components in NextJS
export default async function Page() { const data = await fetch('https://api.example.com/data').then(res => res.json()) return ( <main> <h1>Data:</h1> <pre>{JSON.stringify(data, null, 2)}</pre> </main> ) }
Server components can use await directly because they run on the server.
Use fetch inside server components to get data before rendering.
export default async function UserProfile() { const user = await fetch('https://api.example.com/user').then(res => res.json()) return <div>{user.name}</div> }
export default async function Posts() { const posts = await fetch('https://api.example.com/posts').then(res => res.json()) return ( <ul> {posts.map(post => <li key={post.id}>{post.title}</li>)} </ul> ) }
export default async function Page() { const data = await fetch('https://api.example.com/data', { cache: 'no-store' }).then(res => res.json()) return <pre>{JSON.stringify(data)}</pre> }
This server component fetches current weather data for London from a weather API and shows the temperature and condition.
export default async function Weather() { const response = await fetch('https://api.weatherapi.com/v1/current.json?key=demo&q=London') const weather = await response.json() return ( <main> <h1>Weather in {weather.location.name}</h1> <p>Temperature: {weather.current.temp_c}°C</p> <p>Condition: {weather.current.condition.text}</p> </main> ) }
Server components run only on the server, so you can safely use secrets like API keys here.
Data fetching in server components happens before the page is sent to the browser, so users see content immediately.
Use cache: 'no-store' in fetch options if you want fresh data every time.
Server components fetch data before rendering to improve speed and SEO.
You can use await fetch() directly inside server components.
This keeps your API keys safe and your pages fast.
Practice
Solution
Step 1: Understand server component data fetching
Server components fetch data before rendering, so the page is ready with data when sent to the browser.Step 2: Recognize benefits for speed and SEO
This pre-rendering with data improves page load speed and helps search engines index content better.Final Answer:
Data is fetched before rendering, improving page speed and SEO. -> Option BQuick Check:
Server components fetch data early = D [OK]
- Thinking data fetching happens on client side in server components
- Confusing server components with client components
- Believing no network requests are needed
Solution
Step 1: Identify server component data fetching syntax
Server components support top-level await, so you can directly use await fetch().Step 2: Check each option
const data = await fetch('https://api.example.com/data'); uses await fetch correctly. const data = fetch('https://api.example.com/data').then(res => res.json()); assigns a Promise to data without awaiting. useEffect(() => { fetch('https://api.example.com/data'); }, []); uses useEffect, which is client-side only. const data = fetch('https://api.example.com/data', { method: 'POST' }); uses POST without context, likely incorrect for simple fetch.Final Answer:
const data = await fetch('https://api.example.com/data'); -> Option DQuick Check:
Top-level await fetch in server components = A [OK]
- Using useEffect inside server components
- Not using await with fetch
- Confusing client-side hooks with server code
export default async function Page() {
const res = await fetch('https://api.example.com/user');
const user = await res.json();
return <h1>Hello, {user.name}!</h1>;
}Solution
Step 1: Understand server component data fetching
The fetch call waits for the API response, then parses JSON to get user data.Step 2: Render with fetched data
The component returns an h1 element greeting the user by name, assuming user.name is 'John'.Final Answer:
<h1>Hello, John!</h1> -> Option AQuick Check:
Server component fetch + render data = C [OK]
- Expecting client-side loading states in server components
- Assuming fetch is disallowed in server components
- Not awaiting fetch response before rendering
export default function Page() {
const data = await fetch('https://api.example.com/data');
return <div>Data loaded</div>;
}Solution
Step 1: Check function declaration
The function is not marked async but uses await, which is invalid syntax.Step 2: Understand correct usage
To use await, the function must be async. fetch is allowed in server components, and JSON parsing is optional depending on usage.Final Answer:
Cannot use await inside a non-async function. -> Option CQuick Check:
await requires async function = A [OK]
- Forgetting async keyword on server component
- Thinking fetch is disallowed in server components
- Misusing client hooks like useEffect in server components
export default async function Page() {
const userRes = await fetch('https://api.example.com/user');
const postsRes = await fetch('https://api.example.com/posts');
const user = await userRes.json();
const posts = await postsRes.json();
return <div>{user.name} has {posts.length} posts.</div>;
}Solution
Step 1: Analyze fetch calls in server component
Server components support multiple await fetch calls sequentially as shown.Step 2: Confirm rendering logic
The component renders user.name and posts.length correctly after awaiting JSON parsing.Final Answer:
Correctly fetches both and renders user name with posts count. -> Option AQuick Check:
Multiple awaits in server component allowed = B [OK]
- Thinking useEffect is needed in server components
- Believing multiple fetch calls are disallowed
- Assuming Promise.all is required for correctness
Ready to prove it?
Pass the test and this pattern moves unknown
