A server component is a React component that runs only on the server. It can fetch data and render HTML before sending it to the browser, improving performance and SEO.
Click to reveal answer
beginner
Why keep components on the server in Next.js?
Keeping components on the server helps reduce JavaScript sent to the browser, speeds up page load, and improves SEO by rendering content before it reaches the user.
Click to reveal answer
intermediate
When should you NOT keep a component on the server?
If a component needs to handle user interactions like clicks or form inputs, it should be a client component because server components cannot handle browser events.
Click to reveal answer
intermediate
How do server components affect data fetching?
Server components can fetch data directly on the server without extra client requests, making data loading faster and simpler.
Click to reveal answer
beginner
What is a good real-life example to keep a component on the server?
A blog post list that shows many articles can be a server component because it only needs to display data and does not require user interaction.
Click to reveal answer
Which of these is a reason to keep a component on the server in Next.js?
ATo manage local component state
BTo handle button clicks in the browser
CTo add animations on the client side
DTo improve SEO by pre-rendering content
✗ Incorrect
Server components help improve SEO by rendering HTML on the server before sending it to the browser.
What happens if you try to use browser events in a server component?
AIt runs faster
BIt works normally
CIt causes an error or does not respond
DIt automatically converts to a client component
✗ Incorrect
Server components cannot handle browser events like clicks because they run only on the server.
Which component type fetches data directly on the server in Next.js?
AServer component
BNeither
CBoth client and server components
DClient component
✗ Incorrect
Server components can fetch data on the server, reducing client requests.
When should you choose a client component over a server component?
AWhen you want to improve SEO
BWhen you need user interaction like forms
CWhen you want faster server rendering
DWhen you want to reduce JavaScript size
✗ Incorrect
Client components are needed for user interactions like forms and clicks.
What is a benefit of server components in Next.js?
AThey reduce JavaScript sent to the browser
BThey slow down page load
CThey increase JavaScript bundle size
DThey require client-side data fetching
✗ Incorrect
Server components reduce the JavaScript sent to the browser, improving performance.
Explain when and why you should keep components on the server in Next.js.
Think about performance and user interaction needs.
You got /4 concepts.
Describe the difference between server and client components in Next.js with examples.
Focus on where the code runs and what it can do.
You got /6 concepts.
Practice
(1/5)
1. Which of the following is the best reason to keep a component as a server component in Next.js?
easy
A. It requires frequent updates based on user interaction.
B. It needs to handle user clicks and input events.
C. It fetches data securely and renders static content.
D. It uses browser-only APIs like localStorage.
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 C
Quick Check:
Server components = static + secure data [OK]
Hint: Server components = static or secure data only [OK]
Common Mistakes:
Choosing server component for interactive UI
Using browser APIs in server components
Thinking server components handle events
2. Which syntax correctly forces static rendering for a server component in Next.js 14+?
easy
A. export const dynamic = 'force-static';
B. export const runtime = 'edge';
C. export const dynamic = 'force-dynamic';
D. export const client = true;
Solution
Step 1: Recall server component syntax
Server components can be forced static with export 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 A
Quick Check:
Force static = server component [OK]
Hint: Use 'force-static' to keep component server-only [OK]
Common Mistakes:
Confusing client directive with server component
Using 'force-dynamic' for server-only components
Mixing runtime and dynamic exports
3. Consider this Next.js server component code snippet:
export default function Greeting() {
const name = fetchNameFromDB();
return <h1>Hello, {name}!</h1>;
}
What will be the output rendered in the browser?
medium
A. <h1>Hello, [name from DB]!</h1>
B. Error: fetchNameFromDB is not defined
C. <h1>Hello, undefined!</h1>
D. Nothing renders because server components cannot fetch data
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 B
Quick Check:
Undefined functions cause errors [OK]
Hint: Undefined functions cause errors in server components [OK]
Common Mistakes:
Assuming fetchNameFromDB is defined
Thinking server components can't fetch data
Confusing client-side data fetching with server
4. You have a Next.js component that fetches user data and also handles a button click event. You mistakenly keep it as a server component. What error or issue will you face?
medium
A. The component will render twice causing performance issues.
B. The component will throw a syntax error at build time.
C. The data fetching will fail because server components cannot fetch data.
D. The button click event will not work because server components cannot handle client events.
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 D
Quick Check:
Server components = no client events [OK]
Hint: Client events need client components [OK]
Common Mistakes:
Expecting server components to handle clicks
Thinking server components cause syntax errors
Confusing data fetching ability
5. You want to build a Next.js page that shows a list of blog posts fetched from a database and includes a search box that filters posts as the user types. How should you split components between server and client?
hard
A. Keep the blog list as a server component and the search box as a client component.
B. Make both blog list and search box client components for interactivity.
C. Make both blog list and search box server components for performance.
D. Keep the search box server component and blog list client component.
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 A
Quick Check:
Static data = server, interactive UI = client [OK]