0
0
NextJSframework~10 mins

Use server directive in NextJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Use server directive
Start Component File
Add 'use server' Directive
Define Server-only Functions
Export Component
Next.js Server Processes Component
Server-only Code Runs on Server
Client Receives Rendered Output
Client Interacts with Component
Server Functions Can Be Called via Server Actions
The 'use server' directive marks functions to run only on the server, separating server logic from client code in Next.js components.
Execution Sample
NextJS
"use server";

async function saveData(formData) {
  const data = formData.get("data");
  // server-only logic
}

export default function Page() {
  return (
    <form action={saveData}>
      <input type="hidden" name="data" value="info" />
      <button type="submit">Save</button>
    </form>
  );
}
This code marks saveData as server-only, so it runs on the server when the form is submitted via the button.
Execution Table
StepActionCode EvaluatedResultNotes
1Read file"use server" directive foundMark functions below as server-onlyServer-only functions separated
2Define saveDataasync function saveData(formData)Function saved as server-onlyNot bundled to client
3Export Page componentfunction Page()Component ready for renderingClient and server parts separated
4Render Page on serverJSX <button>Button HTML generatedSent to client
5Client loads pageButton renderedUser sees buttonClient can interact
6User clicks buttonform submission triggers saveData(formData)Server action calledRuns saveData on server
7saveData runssaveData(formData)Data saved on serverNo client code runs saveData
8Return responsesaveData completesClient updates if neededServer-client interaction done
9ExitNo more actionsProcess completeServer directive enforced
💡 Execution stops after server function completes and client updates if needed
Variable Tracker
VariableStartAfter Step 2After Step 6Final
saveDataundefinedFunction defined (server-only)Function called with formDataFunction completes execution
PageundefinedFunction definedRendered on clientComponent remains unchanged
Key Moments - 2 Insights
Why does the saveData function not run on the client?
Because of the 'use server' directive (see execution_table step 1), Next.js treats saveData as server-only and excludes it from client bundles.
How does the client trigger the server-only saveData function?
The client triggers it via a server action call on button click (execution_table step 6), which runs the function on the server.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what happens?
AThe server renders the button HTML to send to the client
BThe client runs the saveData function
CThe saveData function is defined on the client
DThe button is clicked by the user
💡 Hint
Check step 4 in execution_table where rendering happens on the server
At which step does the saveData function run on the server?
AStep 6
BStep 7
CStep 3
DStep 5
💡 Hint
Look at step 7 in execution_table where saveData executes
If the 'use server' directive was removed, what would change in the execution?
AThe button would not render
BThe saveData function would still run on the server
CsaveData would run on the client instead of the server
DThe component would fail to compile
💡 Hint
Refer to variable_tracker and key_moments about server-only function behavior
Concept Snapshot
"use server" directive in Next.js
- Place at top of file to mark server-only functions
- Server-only functions run only on server, not bundled to client
- Client calls server functions via server actions
- Helps separate server logic from client UI
- Improves security and performance by isolating server code
Full Transcript
The 'use server' directive in Next.js marks functions to run only on the server. When Next.js reads a component file with this directive, it separates server-only functions from client code. The server renders the component's HTML and sends it to the client. When the user interacts, such as clicking a button, the client triggers server actions that call the server-only functions. This keeps server logic secure and prevents it from running on the client. The execution table shows each step from reading the directive, defining functions, rendering, client interaction, to server function execution and completion.