Server-only modules are files or code that run only on the server side in Next.js. They never get sent to the browser, keeping sensitive logic or data safe.
Click to reveal answer
intermediate
How do you mark a module as server-only in Next.js?
Import `server-only` at the top of the file (e.g., `import 'server-only';`). Place the module where it is used only in server components or server actions.
Click to reveal answer
beginner
Why should you use server-only modules for database access?
Because database credentials and queries should never be exposed to the browser. Server-only modules keep this code hidden and secure on the server.
Click to reveal answer
intermediate
Can server-only modules import client-side code in Next.js?
No, server-only modules cannot import client-side code because client code depends on browser APIs that don't exist on the server.
Click to reveal answer
intermediate
What happens if you try to use server-only modules in client components?
Next.js will throw an error during build or runtime because server-only modules are not allowed in client components to protect security and avoid runtime issues.
Click to reveal answer
What is the main purpose of server-only modules in Next.js?
ATo improve client-side rendering speed
BTo style components
CTo share code between client and server
DTo keep sensitive code hidden from the browser
✗ Incorrect
Server-only modules keep sensitive logic like database access hidden from the browser.
Where should you place server-only modules in a Next.js app?
AInside the 'pages/api' directory only
BInside the 'public' directory
CInside the 'app' directory and used only in server components
DAnywhere, no restrictions
✗ Incorrect
Server-only modules belong in the 'app' directory and are used only in server components or server actions.
What do you add at the top of a file to mark it as server-only?
A"use client"
Bimport "server-only";
C"use strict"
D"use module"
✗ Incorrect
import "server-only"; marks the module as server-only in Next.js and errors if imported into client code.
Can server-only modules import browser APIs like 'window' or 'document'?
ANo, because those APIs don't exist on the server
BYes, always
COnly if you use dynamic imports
DOnly in development mode
✗ Incorrect
Browser APIs like 'window' or 'document' are not available on the server, so server-only modules cannot use them.
What error occurs if you import a server-only module inside a client component?
ABuild or runtime error preventing the app from running
BSilent failure with no error
CWarning but app still runs
DNo error, it works fine
✗ Incorrect
Next.js throws an error to prevent exposing server-only code in client components.
Explain what server-only modules are in Next.js and why they are important.
Think about what code should never be visible to users.
You got /4 concepts.
Describe how to create and use a server-only module in a Next.js app.
Focus on file location, directives, and usage rules.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of server-only modules in Next.js?
easy
A. To enable server code to be bundled with client JavaScript
B. To run code on both server and client for faster rendering
C. To allow client-side code to access server secrets directly
D. To run code only on the server and keep it hidden from the browser
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 D
Quick Check:
Server-only modules hide code from browser = A [OK]
Hint: Server-only means code runs only on server, hidden from browser [OK]
Common Mistakes:
Thinking server-only modules run on client too
Assuming server-only modules expose secrets to browser
Confusing server-only with shared server-client code
2. Which line correctly marks a Next.js module as server-only?
easy
A. export const client = true;
B. export const runtime = 'edge';
C. import 'server-only';
D. export default function ServerOnly() {}
Solution
Step 1: Recall server-only marking syntax
Next.js uses import '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 C
Quick Check:
Server-only marker = import 'server-only' [OK]
Hint: Look for import 'server-only' to mark server-only [OK]
Common Mistakes:
Using 'edge' runtime which is for edge functions, not server-only
Confusing client export with server-only marking
Missing the import keyword
3. Given this server-only module code in Next.js, what will happen if you try to import it in a client component?
import 'server-only';
export function secretData() {
return 'top-secret';
}
medium
A. The client component will fail to compile with an error
B. The client component will receive the secret data as a string
C. The secretData function will run on the client without errors
D. The secretData function will be bundled into client JavaScript
5. You want to fetch a secret API key only on the server in Next.js and prevent it from being sent to the client. Which approach correctly uses server-only modules to achieve this?