📖 Scenario: You are building a simple Next.js app that fetches user data from an API and displays it on the server side. This helps your app load faster and improves SEO.
🎯 Goal: Create an async server component in Next.js that fetches user data from a public API and renders the user names in a list.
📋 What You'll Learn
Create a server component that fetches data asynchronously
Use the fetch API inside the server component
Render the fetched data as a list of user names
Use Next.js 14+ App Router conventions
💡 Why This Matters
🌍 Real World
Fetching data on the server before rendering pages is common in real-world web apps to improve speed and SEO.
💼 Career
Understanding async server components is essential for modern Next.js development roles focused on performance and user experience.
Progress0 / 4 steps
1
Create the basic server component
Create a file called page.tsx inside the app directory. Inside it, create a default exported async function called Page that returns a simple <div>Loading users...</div> JSX element.
NextJS
Hint
This is the basic structure of a server component in Next.js. It must be an async function to fetch data later.
2
Add the API URL configuration
Inside page.tsx, add a constant called apiUrl and set it to the string "https://jsonplaceholder.typicode.com/users". This will be the URL to fetch user data.
NextJS
Hint
This URL will be used to fetch user data in the next step.
3
Fetch user data asynchronously
Inside the Page function, use await fetch(apiUrl) to get the response. Then use await response.json() to parse the JSON data and store it in a variable called users.
NextJS
Hint
Use await to wait for the fetch and JSON parsing to complete before continuing.
4
Render the list of user names
Replace the <div>Loading users...</div> with a <ul> element. Inside it, use users.map to create a <li> for each user showing their name. Make sure each <li> has a unique key using the user's id.
NextJS
Hint
Use JSX to loop over the users and display their names in a list.
Practice
(1/5)
1. What is the main benefit of using async server components in Next.js?
easy
A. They enable real-time updates without refreshing the page.
B. They allow client-side state management inside the component.
C. They automatically cache data on the client device.
D. They fetch data before rendering, sending ready HTML to the browser.
Solution
Step 1: Understand async server components purpose
Async server components fetch data on the server before rendering the page.
Step 2: Identify the benefit of pre-fetching data
This means the browser receives fully rendered HTML, improving speed and user experience.
Final Answer:
They fetch data before rendering, sending ready HTML to the browser. -> Option D
Quick Check:
Async server components = fetch data first [OK]
Hint: Async server components fetch data before rendering [OK]
Common Mistakes:
Thinking async components run on the client
Confusing caching with async data fetching
Assuming real-time updates happen automatically
2. Which of the following is the correct way to define an async server component in Next.js?
easy
A. async function MyComponent() { const data = await fetchData(); return
{data}
; }
B. function MyComponent() { const data = await fetchData(); return
{data}
; }
C. const MyComponent = () => { const data = await fetchData(); return
{data}
; }
D. const MyComponent = async () => { fetchData().then(data =>
{data}
); }
Solution
Step 1: Identify async function syntax for components
Next.js requires the component function to be marked async to use await inside.
Step 2: Check each option for correct async usage
async function MyComponent() { const data = await fetchData(); return
{data}; } correctly defines an async function and uses await inside. function MyComponent() { const data = await fetchData(); return