Discover how keeping components on the server can make your app lightning fast and simpler to build!
Why When to keep components on server in NextJS? - Purpose & Use Cases
Imagine building a website where every small change means you reload the entire page or write complex code to update parts manually.
You try to keep your UI fast and simple, but managing what runs on the server or client feels confusing and messy.
Manually deciding which parts run on the server or client leads to slow loading, duplicated code, and bugs.
You waste time fixing issues caused by unnecessary client-side work or slow server responses.
Next.js lets you keep components on the server easily, so they load fast and only send what the user needs.
This means less code in the browser, faster pages, and simpler development.
import { useEffect, useState } from 'react'; function Page() { const [data, setData] = useState(null); useEffect(() => { fetchData().then(setData); }, []); return <div>{data}</div>; }
export default async function Page() {
const data = await fetchData();
return <div>{data}</div>;
}You can build fast, scalable apps that send only the needed HTML from the server, improving speed and user experience.
An online store shows product lists rendered on the server so users see content instantly without waiting for client scripts.
Manual control of server vs client code is complex and error-prone.
Keeping components on the server improves speed and simplicity.
Next.js makes server components easy to use for better apps.
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
