In this lesson you will
- Fetch data in an async server component with await
- Read the Cache Components prerender error and choose a fix
- Cache data with "use cache" and set its lifetime with cacheLife
- Stream fresh data with Suspense and a meaningful fallback
Fetching in a server component
In plain React you fetched inside useEffect, kept loading and error in state and drew the page twice. A server component is simpler: make it async, await the data, and return JSX. The request runs on the server, so API keys stay private and the reader never waits for an extra round trip from the browser. The same works for any async work, such as a database query through an ORM.
Two facts about fetch in Next.js 16 matter. First, it is not cached by default: every call really goes to the network. Second, identical fetch calls in one render are memoized, so if a layout and a page both ask for the same URL, the request is made once. That means you can fetch in the component that needs the data instead of passing it down through props.
Now the surprise. New apps have Cache Components on, and the build tries to prerender every page into HTML. The page below awaits an uncached fetch at the top level, so Next.js cannot know whether the rates should be frozen at build time or loaded for each visitor. It stopped the build and listed three ways out. The rest of this lesson takes the first two; the third, export const instant = false, makes the whole page wait for the data on every request and is rarely what you want.
type Rates = { date: string; rates: Record<string, number> };
async function getRates(): Promise<Rates> {
const res = await fetch("https://api.frankfurter.dev/v1/latest?base=USD&symbols=EUR,GBP,INR");
if (!res.ok) throw new Error("Rates request failed: " + res.status);
return res.json();
}
export default async function RatesPage() {
const data = await getRates();
return (
<>
<h1>1 US dollar today</h1>
<p>ECB rates for {data.date}</p>
<ul>
{Object.entries(data.rates).map(([code, value]) => (
<li key={code}>{code}: {value}</li>
))}
</ul>
</>
);
}Error: Route "/rates": Next.js encountered uncached or runtime data during prerendering.
`fetch(...)`, `cookies()`, `headers()`, `params`, `searchParams`, or `connection()` accessed outside of `<Suspense>` prevents the route from being prerendered, blocking the page load and leading to a slower user experience.
Ways to fix this:
- [stream] Provide a placeholder with `<Suspense fallback={...}>` around the data access
- [cache] For uncached data (`fetch`, database calls): cache the access with `"use cache"` (does not apply to `connection()`)
- [block] Set `export const instant = false` to allow a blocking route
Learn more: https://nextjs.org/docs/messages/blocking-prerender-dynamic
Error occurred prerendering page "/rates". Read more: https://nextjs.org/docs/messages/prerender-error
Export encountered an error on /rates/page: /rates, exiting the build.The same message lists cookies(), headers(), params and searchParams: anything that depends on the request needs the same decision.
Sign in free to read the full lesson
You are reading the free preview. Create a free account (it takes one minute, no password) to unlock the rest of this lesson and track your progress.
Unlocks after sign-in:
- 3 more sections with live code examples
- Common mistakes to avoid
- Hands-on coding exercise with automatic checks
- Practice quiz and the graded lesson test
Free forever. No card, no paywall.
Questions people ask
What are Cache Components in Next.js 16?
A rendering model, turned on with cacheComponents: true in next.config.ts and on by default in new apps. Pages are prerendered into a static shell, and every uncached or request-specific read must be cached with "use cache" or wrapped in <Suspense>.
Should I fetch data in useEffect in Next.js?
Usually not. Fetch in a server component so the data is in the HTML, secrets stay on the server and there is no extra loading step. Use client-side fetching only for data that changes while the reader is on the page, often with a library such as SWR.
How long does cacheLife("hours") keep data?
The browser may reuse it for 5 minutes, the server refreshes it in the background after 1 hour, and it expires after 1 day. Other profiles are seconds, minutes, days, weeks and max, and you can define your own in next.config.ts.
Do search engines see the Suspense fallback?
No. Next.js recognises crawlers such as Googlebot, waits for the data and sends them the complete page. People with browsers get the fast shell first and the content a moment later.
Is the Frankfurter exchange rate API free to use?
Yes. It is a free, open source API that serves reference rates published by the European Central Bank, with no key. Rates update once per working day, which is why an hourly cache is plenty.
Sources
Facts in this lesson were checked against these official pages on 25 November 2026.
Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.