Server-only modules let you run code only on the server, keeping it safe and hidden from users. This helps protect secrets and improve performance.
Server-only modules in NextJS
import 'server-only'; // Your server-only code here export async function getServerData() { // fetch or process data const data = {}; // example placeholder return data; }
Use import 'server-only'; at the top to mark the module as server-only.
Server-only modules cannot be imported into client components.
import 'server-only'; export async function getSecret() { return process.env.SECRET_KEY; }
import 'server-only'; export async function fetchData() { const res = await fetch('https://api.example.com/data'); return res.json(); }
This server-only module exports a function that returns the current server time as a string. It runs only on the server, so the time is always accurate and hidden from the client.
import 'server-only'; export async function getServerTime() { return new Date().toISOString(); }
Server-only modules help keep sensitive data safe by never sending it to the browser.
Trying to import server-only modules into client components will cause errors.
Use server-only modules for backend logic, database calls, and secret management.
Server-only modules run only on the server and are hidden from the browser.
They protect secrets and improve app security.
Mark them with import 'server-only'; at the top.
Practice
Solution
Step 1: Understand server-only module purpose
Server-only modules run exclusively on the server and are not sent to the browser.Step 2: Identify security benefit
This keeps secrets safe and improves app security by hiding sensitive code from clients.Final Answer:
To run code only on the server and keep it hidden from the browser -> Option DQuick Check:
Server-only modules hide code from browser = A [OK]
- Thinking server-only modules run on client too
- Assuming server-only modules expose secrets to browser
- Confusing server-only with shared server-client code
Solution
Step 1: Recall server-only marking syntax
Next.js usesimport 'server-only';to mark server-only modules.Step 2: Compare options
import 'server-only'; matches the correct syntax; others either mark edge runtime or client code.Final Answer:
import 'server-only'; -> Option CQuick Check:
Server-only marker = import 'server-only' [OK]
- Using 'edge' runtime which is for edge functions, not server-only
- Confusing client export with server-only marking
- Missing the import keyword
import 'server-only';
export function secretData() {
return 'top-secret';
}Solution
Step 1: Understand server-only import restrictions
Server-only modules cannot be imported into client components; Next.js blocks this to protect secrets.Step 2: Predict behavior on import
Trying to import causes a compile-time error because the module is not allowed in client code.Final Answer:
The client component will fail to compile with an error -> Option AQuick Check:
Importing server-only in client causes error = D [OK]
- Assuming server-only code runs on client
- Thinking secret data is exposed to client
- Believing server-only modules are bundled with client code
export function getSecret() {
return 'hidden';
}What is missing to make this module server-only?
Solution
Step 1: Identify server-only module requirement
Next.js requiresimport 'server-only';to mark a module as server-only.Step 2: Check given code
The code lacks this import, so Next.js treats it as shared or client code, causing errors if used improperly.Final Answer:
You need to add import 'server-only'; at the top -> Option AQuick Check:
Missing 'server-only' import causes error = A [OK]
- Thinking function name controls server-only status
- Assuming React import is needed for server-only modules
- Believing default export affects server-only behavior
// secret.js
import 'server-only';
export function getApiKey() {
return process.env.API_KEY;
}
// page.jsx
'use client';
import { getApiKey } from './secret';
export default function Page() {
const key = getApiKey();
return <p>API Key: {key}</p>;
}Solution
Step 1: Understand server-only module usage
Server-only modules cannot be imported or called directly in client components without error.Step 2: Analyze the code
The page.jsx is a client component calling getApiKey(), which is server-only, causing an error.Step 3: Correct approach
Call getApiKey() inside a server component or server action to keep the secret on the server.Final Answer:
You should move getApiKey call inside a server component or server action -> Option BQuick Check:
Server-only calls must be inside server components = B [OK]
- Calling server-only functions directly in client components
- Exposing secrets by rendering them in client JSX
- Assuming default export fixes server-only usage
Ready to prove it?
Pass the test and this pattern moves unknown
