Fetching data in Server Components

Intermediate · 4 min

Await a fetch right inside an async Server Component, stream slow data behind Suspense, and see why uncached fetches need a boundary under Cache Components.

Before thisServer Components in Next.jsLoading UI and streaming

Codevolution · 4 min

Fetching data in Server Components

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by CodevolutionReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A Server Component can be an async function, so it fetches with plain await and no useEffect, loading flag, or client-side state.
  • fetch requests in Next.js 16 are not cached by default; the response is fetched on each render unless you opt into caching with use cache.
  • With Cache Components on, an uncached fetch that is not inside a Suspense boundary or a use cache scope fails next build with a blocking-route error.
  • Identical fetch GET requests inside one render pass are memoized, so the component that needs the data can fetch it itself instead of receiving it through props.
  • Check res.ok and branch on it; fetch does not throw on a 404 or 500, so the page has to decide what to render.

Why the fetch can sit inside the component

In a client-only React app, a component cannot wait. It renders once with nothing, then an effect fires after mount, the request goes out, state updates, and it renders again. Every piece of that dance (the loading flag, the effect, the cleanup) exists because the component runs in a place where waiting is not allowed.

A Server Component runs before any HTML reaches the browser, so waiting is fine. It can be an async function. You await the data, then return markup that already contains it.

// app/blog/page.tsx
export default async function Page() {
  const res = await fetch('https://api.vercel.app/blog')
  if (!res.ok) return <p>Could not load posts.</p>

  const posts: { id: number; title: string }[] = await res.json()

  return (
    <ul>
      {posts.map((post) => (
        <li key={post.id}>{post.title}</li>
      ))}
    </ul>
  )
}

No hook, no loading state in the component, no JSON shipped to the client that the browser then has to turn into UI. The browser gets HTML.

The res.ok check matters. fetch only rejects when the network fails. A 404 or a 500 resolves like any other response, so without the check you would feed an error body to .json() and map over something that is not an array.

What fetch does and does not cache in Next.js 16

Older Next.js versions cached fetch results on the server by default, which surprised almost everyone. That is gone. In Next.js 16, fetch requests are not cached by default. A plain fetch runs again whenever the component renders.

Two things still happen automatically:

  • Identical GET requests (same URL, same options) inside one render pass are memoized. Two components that call the same endpoint produce one network request. The next lessons cover this.
  • Nothing persists across requests unless you ask for it with use cache.

Slow data and the blocking-route error

A component that awaits a slow request holds up everything that depends on it. If the whole page waits on that component, the visitor stares at a blank screen.

The fix is to isolate the slow part behind <Suspense>, so the rest of the page can be sent first and the posts stream in when they are ready:

import { Suspense } from 'react'

async function Posts() {
  const res = await fetch('https://api.vercel.app/blog')
  const posts: { id: number; title: string }[] = await res.json()
  return (
    <ul>
      {posts.map((p) => (
        <li key={p.id}>{p.title}</li>
      ))}
    </ul>
  )
}

export default function Page() {
  return (
    <>
      <h1>Blog</h1>
      <Suspense fallback={<p>Loading posts…</p>}>
        <Posts />
      </Suspense>
    </>
  )
}

With Cache Components enabled (the default in a project created by create-next-app 16.4), this is more than a nicety. If Posts awaited its fetch with no <Suspense> above it and no use cache, next build stops with an error that starts “Next.js encountered uncached or runtime data during prerendering”. I triggered the same error with a cookies() read outside a boundary on 16.4.6. The message lists three ways out: stream it behind Suspense, cache it, or opt the route into blocking with export const instant = false. A loading.tsx file gives you the whole-page version of the same boundary; see loading UI and streaming.

Fetch where the data is used

Because identical requests are memoized within a render, you do not need to fetch at the top of the tree and pass everything down through props. A <Comments> component can fetch its own comments and a <Header> can fetch the current user. If both ask for the same user, the request goes out once. Keep the fetching next to the markup that uses it, and the data flow stays easy to read.

What the video covers

Codevolution’s short Next.js 15 tutorial shows the pattern above end to end: it explains async and await in a Server Component, sets up a project with typed data from a mock API, fetches and renders it, and has a section on handling multiple fetch requests. It was recorded on Next.js 15, so it does not mention Cache Components or the blocking-route build error; this lesson covers those.

Where this fits

Fetching from an API is half the picture. Plenty of apps own their data and can skip HTTP entirely. Next: querying a database directly. For the React side of the same ideas, see suspense for data fetching.

Check yourself

  1. 01Why does a Server Component not need useEffect or useState to load data?

    It runs on the server before any HTML is sent, so it can be an async function that awaits the fetch and returns finished markup, while useEffect and useState exist to run code after a component mounts in the browser.

  2. 02In Next.js 16, is a fetch call inside a Server Component cached by default?

    No. fetch requests are not cached by default, so a plain fetch is re-run on every render, and you opt into caching by wrapping the work in a function or component that uses the use cache directive.

  3. 03What happens at build time if a page with Cache Components enabled awaits an uncached fetch outside any Suspense boundary?

    The build fails with an error saying Next.js encountered uncached or runtime data during prerendering, because the route could not produce a static shell; the fixes are to wrap the component in Suspense or cache the data with use cache.

  4. 04Why does the code in this lesson check res.ok before calling res.json()?

    fetch only rejects on network failure, so a 404 or 500 still resolves normally and the page would try to render an error body as data.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark