Server Components: Fetch Data Directly
Server Components let you fetch data directly using async/await, simplifying code by removing client-side useEffect hooks. Use this for initial page loads in Next.js to improve SEO and performance. The footgun: don't use client-side hooks or browser APIs.
Why it exists
Traditional client-side fetching in React apps often involves complex state management with hooks like useEffect and useState to handle loading, error, and data states. This can lead to content layout shifts as data arrives after the initial page load and can negatively impact SEO since search engine crawlers may not see the final content.
The mental model
Treat a Server Component like a server-side script that outputs HTML. You can make the component's function async and await data from any source—a database, an external API, or the local filesystem—directly within its body. React and Next.js handle the underlying plumbing of fetching, caching the result, and streaming the final HTML to the client.
How it works
When a user requests a page, Next.js begins rendering the component tree on the server. If it encounters an async Server Component, rendering for that component and its children is paused until the awaited data fetch (the Promise) resolves. Next.js extends the native fetch API, automatically deduplicating identical requests and providing granular caching control. Once the data is available, the component finishes rendering its HTML, which is then streamed to the browser. This can be combined with React Suspense to show a loading fallback while data is being fetched.
When to use it
This is the default and recommended pattern for fetching the initial data required to render a page in the Next.js App Router. Use it whenever you need to get data from a database, a CMS, or any backend API. It's especially powerful for accessing resources that require secret keys or credentials, as those are never exposed to the client's browser.
When not to use it
This pattern is for server-side rendering. Do not use it for data that needs to be fetched or re-fetched based on client-side user interactions that happen after the page has loaded, such as live search, sorting a table, or submitting a form. Those scenarios are better handled by Client Components using traditional fetching methods or by using Server Actions.
One canonical example
A product details page component can be an async function that receives a product ID from the URL params. Inside the component, it can await a call to a database or an e-commerce API to get the product's name, price, and description. It then directly renders this data into JSX. The entire process happens on the server, and the client receives a fully-formed HTML page.
Interview question
For which scenario would fetching data directly within a Server Component be LEAST appropriate?
- a.Updating a filterable product list in real-time as a user interacts with checkboxes.Correct
- b.Displaying sensitive user data retrieved from a secure backend.
- c.Initial rendering of a blog post's content from a CMS.
- d.Populating an e-commerce product page with details from a database.
Why? this is the answer
The card states that Server Components should not be used for data fetched or re-fetched based on client-side user interactions after the page loads, which includes dynamic filtering. Options A, B, and C are all examples of initial data fetching from various backend sources, which is the primary recommended use case.
Just read this? Test yourself on what you have been reading.
Read the original → nextjs.org
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on react — each one lists the topics its interview covers.
See open roles