📖 Scenario: You are building a Next.js app that fetches secret data from a server-only module. This module should never be exposed to the browser for security reasons.
🎯 Goal: Create a server-only module that exports a secret message. Then, import and use this module in a server component to display the secret message safely.
📋 What You'll Learn
Create a server-only module file named secretMessage.ts exporting a constant string secret with value 'This is a secret from the server'.
Create a server component named SecretDisplay that imports secret from secretMessage.ts.
Render the secret message inside a <div> with an accessible aria-label describing it as 'Secret message'.
Ensure the server-only module is not imported in any client component.
💡 Why This Matters
🌍 Real World
Server-only modules are used to keep sensitive data or logic hidden from the browser, such as API keys, database queries, or secret messages.
💼 Career
Understanding server-only modules is important for building secure Next.js applications and working with server components effectively in professional web development.
Progress0 / 4 steps
1
Create the server-only module
Create a file named secretMessage.ts and export a constant called secret with the exact string value 'This is a secret from the server'.
NextJS
Hint
Use export const secret = 'This is a secret from the server'; in secretMessage.ts.
2
Create the server component
Create a React server component named SecretDisplay in a file SecretDisplay.tsx. Import secret from ./secretMessage.
NextJS
Hint
Define a function component that returns a <div> with aria-label="Secret message" and displays {secret}.
3
Use the server component in a page
In the app/page.tsx file, import the SecretDisplay component from ./SecretDisplay and render it inside the default exported function.
NextJS
Hint
Render <SecretDisplay /> inside a <main> element in the page component.
4
Confirm server-only module usage
Ensure that secretMessage.ts is only imported in server components like SecretDisplay.tsx and not in any client components. Add a comment in secretMessage.ts stating it is server-only.
NextJS
Hint
Add a comment // This module is server-only at the top of secretMessage.ts.
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?