Server components let you run code only on the server, so your web page sends less code to the browser. This makes your app faster and smaller.
Zero bundle size for server components in NextJS
export default function MyServerComponent() { // This code runs only on the server return <div>Hello from server!</div>; }
Server components are React components that run only on the server.
They do not include client-side JavaScript in the browser bundle.
export default function ServerComponent() { return <p>Rendered on server only</p>; }
import { fetchData } from '@/lib/data'; export default async function ServerComponent() { const data = await fetchData(); return <pre>{JSON.stringify(data, null, 2)}</pre>; }
"use client"; export default function ClientComponent() { return <button>Click me</button>; }
This server component fetches a user name on the server and renders a greeting. No JavaScript for this component is sent to the browser, so the page loads faster.
import React from 'react'; // This is a server component export default async function Greeting() { // Simulate fetching user name on server const userName = await Promise.resolve('Alice'); return ( <main> <h1>Hello, {userName}!</h1> <p>This text is rendered on the server only.</p> </main> ); }
Server components cannot use browser-only APIs like window or document.
They help reduce the JavaScript bundle size sent to the client.
Use client components only when you need interactivity like event handlers.
Server components run only on the server and send zero JavaScript to the browser.
This reduces bundle size and improves page load speed.
Use server components for static or data-fetching UI parts without interactivity.
Practice
Solution
Step 1: Understand server components behavior
Server components run only on the server and do not send JavaScript to the browser.Step 2: Identify the benefit of zero bundle size
Because no JavaScript is sent, the bundle size is smaller, which improves page load speed.Final Answer:
They send no JavaScript to the browser, reducing bundle size and improving load speed. -> Option AQuick Check:
Zero bundle size = faster load [OK]
- Thinking server components run on the client
- Assuming server components add client interactivity
- Confusing caching with zero bundle size
Solution
Step 1: Identify server component syntax
By default, components without "use client" directive are server components in Next.js.Step 2: Check the options for client/server directives
export default function MyComponent() { return <div>Hello</div>; }has no "use client" directive, so it is a server component with zero bundle size.Final Answer:
export default function MyComponent() { return <div>Hello</div>; } -> Option CQuick Check:
No "use client" means server component [OK]
- Adding "use client" which makes it a client component
- Using "use server" which is not a valid directive
- Assuming import React affects server/client distinction
export default function Greeting() {
const message = 'Hello from server';
return <h1>{message}</h1>;
}Solution
Step 1: Understand server component rendering
Server components render HTML on the server and send only HTML to the browser, no JS bundle.Step 2: Analyze the code output
The component returns an <h1> with the message string, so the browser receives that HTML.Final Answer:
An HTML <h1> element with text 'Hello from server' and no JavaScript bundle. -> Option AQuick Check:
Server component sends HTML only [OK]
- Thinking server components send JS for hydration
- Assuming server components render nothing
- Confusing client components with server components
"use client";
export default function UserName() {
const name = fetchUserName();
return <div>{name}</div>;
}What is the main issue here?
Solution
Step 1: Identify component type from directive
"use client" makes this a client component, so server-only functions like fetchUserName() cannot run here.Step 2: Understand server-only function usage
fetchUserName() likely fetches data on the server, so calling it in a client component causes an error.Final Answer:
Using "use client" makes it a client component, but fetchUserName() is a server-only function causing an error. -> Option BQuick Check:
Client component cannot call server-only functions [OK]
- Forgetting "use client" changes component type
- Assuming fetchUserName() works on client
- Thinking async keyword fixes server/client mismatch
Solution
Step 1: Identify zero bundle size requirement
Zero bundle size means no JavaScript sent to browser, so no client components.Step 2: Choose correct data fetching method
Fetching data inside a server component without "use client" sends only HTML with data, no JS bundle.Step 3: Evaluate other options
Client components or getStaticProps with client components send JS bundles, so they don't meet zero bundle size.Final Answer:
Create a server component that fetches data inside and returns JSX, without "use client" directive. -> Option DQuick Check:
Server component + data fetch = zero bundle size [OK]
- Adding "use client" which sends JS bundle
- Using client-side data fetching hooks
- Assuming getStaticProps alone ensures zero bundle size
Ready to prove it?
Pass the test and this pattern moves unknown
