Server Components are React components that run only on the server. They can fetch data, access the file system, and do heavy work without sending code to the browser.
Click to reveal answer
beginner
Can Server Components fetch data directly from a database?
Yes. Server Components can fetch data directly from databases or APIs because they run on the server where these resources are accessible.
Click to reveal answer
beginner
Can Server Components use browser-only APIs like localStorage or window?
No. Server Components run on the server and do not have access to browser-only APIs like localStorage or window.
Click to reveal answer
intermediate
What kind of code can run inside Server Components?
Any code that runs on the server can run inside Server Components, such as file system access, environment variables, and server-side logic.
Click to reveal answer
intermediate
Why should you use Server Components in Next.js?
They improve performance by reducing JavaScript sent to the browser and allow secure data fetching and processing on the server.
Click to reveal answer
Where do Next.js Server Components run?
AIn the browser only
BOn the server only
CBoth server and browser
DOn a separate client app
✗ Incorrect
Server Components run only on the server, not in the browser.
Which of these can Server Components do?
AAccess the file system
BUse localStorage
CListen to browser events
DManipulate the DOM directly
✗ Incorrect
Server Components can access the file system because they run on the server.
Can Server Components use React hooks like useState?
AOnly useState works
BYes, all React hooks work
CNo, useState is not supported in Server Components
DOnly useEffect works
✗ Incorrect
Server Components do not support client-side hooks like useState or useEffect.
Why avoid using browser APIs in Server Components?
AThey slow down the server
BThey are too complex
CThey are deprecated
DThey cause errors because server has no browser environment
✗ Incorrect
Browser APIs like window or localStorage do not exist on the server, causing errors.
What is a benefit of Server Components in Next.js?
ALess JavaScript sent to the browser
BMore client-side rendering
CFaster animations
DBetter offline support
✗ Incorrect
Server Components reduce JavaScript sent to the browser, improving load speed.
Explain what kinds of code can run inside Next.js Server Components and why.
Think about what the server environment allows that the browser does not.
You got /4 concepts.
Describe the main advantages of using Server Components in Next.js applications.
Consider speed, security, and user experience benefits.
You got /4 concepts.
Practice
(1/5)
1. Which of the following can you safely run inside a Next.js server component?
easy
A. Manipulating the DOM directly
B. Accessing a database directly
C. Adding event listeners for user clicks
D. Using browser APIs like localStorage
Solution
Step 1: Understand server component environment
Server components run only on the server, so they can use server-only features like database access.
Step 2: Identify client-only features
Browser APIs, event listeners, and DOM manipulation require client-side JavaScript and cannot run in server components.
Final Answer:
Accessing a database directly -> Option B
Quick Check:
Server-only code = Access database [OK]
Hint: Server components run server code only, no browser APIs [OK]
Common Mistakes:
Thinking client APIs like localStorage work in server components
Trying to add event listeners in server components
Assuming DOM manipulation is possible on server
2. Which syntax correctly imports a server-only module inside a Next.js server component?
easy
A. import localStorage from 'localStorage';
B. import { useState } from 'react';
C. import window from 'window';
D. import fs from 'fs';
Solution
Step 1: Recognize server-only modules
Modules like 'fs' (file system) are server-only and can be imported in server components.
Step 2: Identify invalid imports
useState is client-side React hook, window and localStorage are browser globals, not importable modules.
Final Answer:
import fs from 'fs'; -> Option D
Quick Check:
Server module import = fs [OK]
Hint: Only import Node.js modules like fs in server components [OK]
Common Mistakes:
Importing client hooks like useState in server components
Trying to import browser globals as modules
Confusing client and server imports
3. Given this server component code, what will be rendered?
import fs from 'fs';
export default function Page() {
const data = fs.readFileSync('data.txt', 'utf8');
return <div>{data}</div>;
}
medium
A. Contents of data.txt file inside a div
B. Error: fs module not found
C. Empty div with no content
D. Client-side error about fs usage
Solution
Step 1: Understand server component execution
The code runs on the server, so fs.readFileSync reads the file content successfully.
Step 2: Render the file content
The content read from 'data.txt' is returned inside a div and sent as HTML to the client.
Final Answer:
Contents of data.txt file inside a div -> Option A
Quick Check:
Server read file = content rendered [OK]
Hint: Server components can read files and render content directly [OK]
B. onClick handlers are not allowed in server components
C. useState cannot be used in server components
D. No error, code works fine
Solution
Step 1: Check React hooks usage in server components
useState is a client-side hook and cannot be used in server components.
Step 2: Check event handlers in server components
onClick handlers require client-side JavaScript and are not supported in server components.
Final Answer:
Both A and B -> Option A
Quick Check:
useState + onClick invalid in server components [OK]
Hint: No React hooks or event handlers in server components [OK]
Common Mistakes:
Using useState in server components
Adding client event handlers in server components
Assuming server components can handle user events
5. You want to fetch data from a database and display it in a Next.js app. Which approach best uses server components to improve performance and security?
hard
A. Fetch data in the browser using fetch API directly
B. Fetch data inside a client component using useEffect
C. Fetch data inside a server component and pass it as props to a client component
D. Fetch data in a client component and store it in localStorage
Solution
Step 1: Understand server component advantages
Server components can securely fetch data on the server, avoiding exposing secrets or extra client JS.
Step 2: Use server component to fetch and pass data
Fetching data in server component and passing it as props to client components optimizes performance and security.
Final Answer:
Fetch data inside a server component and pass it as props to a client component -> Option C
Quick Check:
Server fetch + props passing = best practice [OK]
Hint: Fetch data server-side, then pass to client components [OK]
Common Mistakes:
Fetching data only on client causing slower load
Exposing secrets by fetching in client
Storing sensitive data in localStorage
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.