Discover how fetching data on the server can make your web pages feel instant and smooth!
Why Fetch in server components in NextJS? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine building a web page where you manually fetch data on the client side after the page loads, causing a delay and flicker as content appears.
Fetching data manually on the client can cause slow loading, extra network requests, and a poor user experience with visible loading states and layout shifts.
Fetching data directly in server components lets the server gather all needed data before sending the page, so users see fully loaded content instantly without flicker.
useEffect(() => { fetch('/api/data').then(res => res.json()).then(setData); }, []);const data = await fetch('https://api.example.com/data').then(res => res.json());This approach enables fast, seamless pages that load complete data on the server, improving speed and user experience.
Think of an online store homepage that shows product lists immediately without loading spinners or layout jumps.
Manual client fetching causes delays and flicker.
Server components fetch data before rendering.
Users get faster, smoother page loads.
Practice
fetch() call to get data before rendering?Solution
Step 1: Understand server components run on the server
Server components execute on the server before sending HTML to the browser, so data fetching happens here.Step 2: Use
Usingawait fetch()inside async server componentsawait fetch()directly inside an async server component fetches data before rendering.Final Answer:
Directly inside the async server component usingawait fetch()-> Option CQuick Check:
Fetch in async server component = Directly inside the async server component usingawait fetch()[OK]
- Trying to fetch data inside client hooks like useEffect
- Fetching data in React state hooks which run on client
- Assuming API routes are mandatory for server component fetch
Solution
Step 1: Use await with fetch to wait for response
Fetch returns a promise, so useawaitto wait for it to resolve.Step 2: Call
After fetch resolves, callres.json()on the response to parse JSONres.json()to get the parsed data, also awaited or chained with then.Final Answer:
const data = await fetch('https://api.example.com/data').then(res => res.json()) -> Option AQuick Check:
Await fetch then parse JSON = const data = await fetch('https://api.example.com/data').then(res => res.json()) [OK]
- Forgetting to await fetch causing unresolved promises
- Calling .json() directly on fetch without awaiting response
- Using fetch without handling the response parsing
export default async function Page() {
const res = await fetch('https://jsonplaceholder.typicode.com/todos/1');
const todo = await res.json();
return {JSON.stringify(todo)};
}Solution
Step 1: Await fetch and parse JSON in server component
The code correctly awaits fetch and then awaits res.json() to get the todo object.Step 2: Return JSON stringified todo inside <pre> tag
The component returns a <pre> element with the stringified todo data, so it renders the JSON text.Final Answer:
<pre>{"userId":1,"id":1,"title":"delectus aut autem","completed":false}</pre> -> Option AQuick Check:
Await fetch + JSON.stringify output ={"userId":1,"id":1,"title":"delectus aut autem","completed":false}[OK]
- Expecting fetch to fail in server components
- Not awaiting fetch causing unresolved promises
- Returning raw object instead of stringified JSON
export default function Page() {
const res = await fetch('https://api.example.com/data');
const data = await res.json();
return <div>{data.title}</div>;
}Solution
Step 1: Check async usage in server component
The component uses await but is not declared async, which causes a syntax error.Step 2: Confirm fetch is allowed in server components
Fetch is allowed in server components, so no error there. Also, res.json() returns a promise, so awaiting is correct.Final Answer:
Missing async keyword on the component function -> Option BQuick Check:
Await requires async function = Missing async keyword on the component function [OK]
- Forgetting async on function using await
- Thinking fetch is disallowed in server components
- Misunderstanding res.json() returns a promise
Solution
Step 1: Understand parallel fetching improves performance
Fetching user and posts in parallel with Promise.all reduces total wait time compared to sequential awaits.Step 2: Use Promise.all inside async server component to fetch both
Inside the async server component, useconst [user, posts] = await Promise.all([fetchUser(), fetchPosts()])to fetch simultaneously.Step 3: Render user name and post titles after both fetches complete
After both promises resolve, render the data together efficiently in the server component.Final Answer:
Fetch user and posts in parallel using Promise.all inside the server component -> Option DQuick Check:
Parallel fetch with Promise.all = Fetch user and posts in parallel using Promise.all inside the server component [OK]
- Fetching data sequentially causing slower load
- Mixing client and server fetching unnecessarily
- Nesting fetch calls causing callback delays
