0
0
NextJSframework~20 mins

Fetch in server components in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Next.js Server Fetch Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Next.js server component fetching data?

Consider this Next.js server component that fetches a list of users from an API and renders their names:

export default async function Users() {
  const res = await fetch('https://jsonplaceholder.typicode.com/users');
  const users = await res.json();
  return (
    <ul>
      {users.map(user => <li key={user.id}>{user.name}</li>)}
    </ul>
  );
}

What will this component render?

NextJS
export default async function Users() {
  const res = await fetch('https://jsonplaceholder.typicode.com/users');
  const users = await res.json();
  return (
    <ul>
      {users.map(user => <li key={user.id}>{user.name}</li>)}
    </ul>
  );
}
A<ul><li>undefined</li></ul>
B<ul></ul>
CError: fetch is not defined
D<ul><li>Leanne Graham</li><li>Ervin Howell</li><li>Clementine Bauch</li>... (all 10 users)</ul>
Attempts:
2 left
💡 Hint

Think about what the fetch call returns and how the data is used in the component.

lifecycle
intermediate
2:00remaining
When does the fetch run in a Next.js server component?

Given a server component that fetches data using await fetch(), when does this fetch happen?

NextJS
export default async function Data() {
  const res = await fetch('https://api.example.com/data');
  const data = await res.json();
  return <div>{data.message}</div>;
}
AAt build time only, never on client requests
BOn every client request to the page or component
COnly once when the server starts
DOnly when the user clicks a button
Attempts:
2 left
💡 Hint

Think about how server components behave on each request.

📝 Syntax
advanced
2:00remaining
Which fetch usage in a server component causes a runtime error?

Identify which fetch usage will cause a runtime error in a Next.js server component.

NextJS
export default async function Example() {
  // Option A
  const resA = await fetch('/api/data');

  // Option B
  const resB = await fetch('https://api.example.com/data', { cache: 'no-store' });

  // Option C
  const resC = await fetch('https://api.example.com/data', { method: 'POST' });

  // Option D
  const resD = await fetch('https://api.example.com/data', { signal: new AbortController().signal });

  return <div>Check console</div>;
}
Afetch with method: 'POST' causes runtime error in server component
Bfetch('/api/data') without full URL causes runtime error
Cfetch with cache: 'no-store' works fine
Dfetch with AbortController signal works fine
Attempts:
2 left
💡 Hint

Consider which HTTP methods are allowed in server component fetch calls.

🔧 Debug
advanced
2:00remaining
Why does this server component fetch fail with 'TypeError: Failed to fetch'?

Look at this server component code:

export default async function Posts() {
  const res = await fetch('http://localhost:3000/api/posts');
  const posts = await res.json();
  return (
    <ul>
      {posts.map(post => <li key={post.id}>{post.title}</li>)}
    </ul>
  );
}

Why does it fail with TypeError: Failed to fetch when deployed?

NextJS
export default async function Posts() {
  const res = await fetch('http://localhost:3000/api/posts');
  const posts = await res.json();
  return (
    <ul>
      {posts.map(post => <li key={post.id}>{post.title}</li>)}
    </ul>
  );
}
AAPI returns invalid JSON causing fetch to fail
BMissing await before fetch causes failure
CUsing 'localhost' URL in fetch fails in deployed environment
Dfetch requires HTTPS URL always
Attempts:
2 left
💡 Hint

Think about what 'localhost' means when deployed on a server.

🧠 Conceptual
expert
2:00remaining
What is the main benefit of using fetch in Next.js server components over client components?

Choose the best explanation for why fetching data in server components is preferred over client components in Next.js.

AServer components fetch data on the server, reducing client bundle size and improving SEO
BClient components fetch data faster because they run in the browser
CServer components fetch data only once at build time, so data is always static
DClient components can fetch data without any network delay
Attempts:
2 left
💡 Hint

Think about where the code runs and how it affects performance and SEO.