Let's learn about server components in Next.js. This is our favourite topic of discussion and we have been referring it throughout the course.
In this lesson, let's go over what Server Components are, how they work, and how they are different from Client Components.
Server Components are rendered on the server, while Client Components are rendered in the browser. During a build, Next.js finds the Server Components, fetches the data they need and turns the result into a stream called the RSC Payload (React Server Components Payload). When the browser requests a page, React combines that payload with the Client Components to update the DOM.
window object or event handlers.use client directive. They can re-render, are included in the browser bundle and can use hooks like useState, useEffect and useCallback. Using any of these hooks means the component must be a Client Component.use client component also becomes a Client Component, so you'll want to extract parts that don't need to be on the client.Now that we understand what Server and Client components are, let's see how we can create a Server Component.
For example, if you create a component like this:
Like I mentioned earlier, if you don't add the use client directive to a component, it will be a Server Component by default in the Next.js app router.
Here are some of the benefits of using Server Components:
| Benefit | Why It Matters |
|---|---|
| 💨Less JavaScript | Smaller bundles, faster load, better performance |
| 🔒No data leaks to client | You can access secrets, DB, and private APIs directly in components |
| 🧠Simpler code | No need for fetch(), useEffect, or APIs just to show a list of items |
| 📦Better UX | Streaming + partial updates = faster perceived response times |
Here are some of the cons of using Server Components:
| Benefit | Why It Matters |
|---|---|
| ⏱️No Client-Side Interactivity | Cannot use browser APIs, event handlers, or React hooks like useState/useEffect |
| 💰Server Cost | More server resources are used and can increase server load and costs |
| You can co-locate UI with data-fetching logic without worrying about JS bloat |
export default function MyComponent() {
return <div>Hello</div>;
}