Keeping components on the server helps your app load faster and keeps data safe. It means the server does the work, and the user gets ready-to-see pages quickly.
When to keep components on server in NextJS
export default function Page() { // This is a server component by default in Next.js 13+ return <h1>Hello from the server!</h1>; }
In Next.js 13+, components inside the app/ folder are server components by default.
You do not need to add special code to make a component run on the server.
export default function ServerComponent() { return <p>This runs on the server only.</p>; }
"use client"; at the top makes this a client component instead."use client"; export default function ClientComponent() { return <button>Click me</button>; }
export default async function ServerComponent() { const data = await fetch('https://api.example.com/data'); const json = await data.json(); return <pre>{JSON.stringify(json, null, 2)}</pre>; }
This server component checks the current time on the server and shows a greeting. It runs on the server, so the user sees the correct greeting immediately.
export default async function ServerGreeting() { const hour = new Date().getHours(); const greeting = hour < 12 ? 'Good morning' : 'Good afternoon'; return <h1>{greeting}, visitor!</h1>; }
Server components cannot use browser-only features like window or document.
Use client components only when you need interactivity like buttons or forms.
Server components help keep your app fast and secure by doing work before sending pages to users.
Server components run on the server and send ready HTML to the browser.
Use them for static content, data fetching, and security.
Client components are for interactive parts that need the browser.
Practice
Solution
Step 1: Understand server component purpose
Server components run on the server and are ideal for static content and secure data fetching.Step 2: Identify interactive needs
Components that handle user clicks or browser APIs must be client components, not server components.Final Answer:
It fetches data securely and renders static content. -> Option CQuick Check:
Server components = static + secure data [OK]
- Choosing server component for interactive UI
- Using browser APIs in server components
- Thinking server components handle events
Solution
Step 1: Recall server component syntax
Server components can be forced static withexport const dynamic = 'force-static';.Step 2: Differentiate other options
export const dynamic = 'force-dynamic'; (dynamic = 'force-dynamic') forces dynamic rendering, B (runtime = 'edge') sets edge runtime without forcing static, D is invalid syntax.Final Answer:
export const dynamic = 'force-static'; -> Option AQuick Check:
Force static = server component [OK]
- Confusing client directive with server component
- Using 'force-dynamic' for server-only components
- Mixing runtime and dynamic exports
export default function Greeting() {
const name = fetchNameFromDB();
return <h1>Hello, {name}!</h1>;
}What will be the output rendered in the browser?
Solution
Step 1: Understand server component data fetching
Server components can fetch data directly on the server before rendering.Step 2: Analyze the code behavior
The function fetchNameFromDB() is not defined in the snippet, so this will cause a runtime error.Final Answer:
Error: fetchNameFromDB is not defined -> Option BQuick Check:
Undefined functions cause errors [OK]
- Assuming fetchNameFromDB is defined
- Thinking server components can't fetch data
- Confusing client-side data fetching with server
Solution
Step 1: Understand event handling in Next.js components
Only client components can handle browser events like button clicks.Step 2: Identify the problem with server components
Server components run on the server and cannot respond to client-side events, so the button click won't work.Final Answer:
The button click event will not work because server components cannot handle client events. -> Option DQuick Check:
Server components = no client events [OK]
- Expecting server components to handle clicks
- Thinking server components cause syntax errors
- Confusing data fetching ability
Solution
Step 1: Identify static and interactive parts
The blog list is static content fetched from the database, suitable for server components.Step 2: Identify interactive UI needs
The search box needs to respond to user typing, so it must be a client component.Step 3: Combine components properly
Use server component for data fetching and rendering list, client component for search input and filtering.Final Answer:
Keep the blog list as a server component and the search box as a client component. -> Option AQuick Check:
Static data = server, interactive UI = client [OK]
- Making everything client component unnecessarily
- Trying to handle user input in server component
- Mixing up which part fetches data
Ready to prove it?
Pass the test and this pattern moves unknown
