Discover how running code only on the server can make your web apps faster and safer without extra hassle!
Why What can run in server components in NextJS? - Purpose & Use Cases
Imagine building a web page where you manually fetch data on the server, then send it to the client, and finally update the page with JavaScript. You have to write separate code for server and client, and keep track of what runs where.
This manual split is confusing and slow. You might accidentally run server-only code on the client, causing errors. Managing data fetching and rendering separately wastes time and makes your code messy.
Next.js Server Components let you write components that run only on the server. They can fetch data, access secure resources, and prepare HTML before sending it to the client. This keeps your code clean and fast without extra client-side work.
fetchData(); renderClientSide();
export default function ServerComponent() { const data = fetchData(); return <div>{data}</div>; }You can build fast, secure pages that load data on the server and send ready HTML to the browser, improving performance and developer experience.
Imagine an online store page that fetches product details from a database securely on the server, then sends the complete page to the user without exposing database keys or waiting for client scripts.
Server Components run only on the server, not in the browser.
They can safely fetch data and access secrets without exposing them.
This leads to faster, simpler, and more secure web pages.
Practice
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 BQuick Check:
Server-only code = Access database [OK]
- Thinking client APIs like localStorage work in server components
- Trying to add event listeners in server components
- Assuming DOM manipulation is possible on server
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 DQuick Check:
Server module import = fs [OK]
- Importing client hooks like useState in server components
- Trying to import browser globals as modules
- Confusing client and server imports
import fs from 'fs';
export default function Page() {
const data = fs.readFileSync('data.txt', 'utf8');
return <div>{data}</div>;
}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 AQuick Check:
Server read file = content rendered [OK]
- Thinking fs is unavailable in server components
- Expecting client-side errors for server code
- Assuming empty output without file content
export default function Page() {
const [count, setCount] = useState(0);
return <button onClick={() => setCount(count + 1)}>Count: {count}</button>;
}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 AQuick Check:
useState + onClick invalid in server components [OK]
- Using useState in server components
- Adding client event handlers in server components
- Assuming server components can handle user events
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 CQuick Check:
Server fetch + props passing = best practice [OK]
- Fetching data only on client causing slower load
- Exposing secrets by fetching in client
- Storing sensitive data in localStorage
Ready to prove it?
Pass the test and this pattern moves unknown
