0
0
NextJSframework~5 mins

Use server directive in NextJS - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the use server directive do in Next.js?
It marks a function or component to run only on the server side, ensuring it does not run in the browser. This helps with security and performance.
Click to reveal answer
beginner
Where should you place the use server directive in your Next.js code?
At the very top of the file or function before any other code, as a special directive line: 'use server'.
Click to reveal answer
intermediate
Why is it important to use the use server directive for server-only functions?
It prevents server-only code from being bundled and sent to the browser, reducing bundle size and avoiding exposing sensitive logic.
Click to reveal answer
beginner
Can a component marked with use server access browser APIs like window?
No. Components or functions with use server run only on the server and do not have access to browser-specific APIs.
Click to reveal answer
intermediate
How does use server relate to React Server Components in Next.js?
It explicitly marks functions or components as server-only, which is a key part of React Server Components to separate server and client logic.
Click to reveal answer
What is the correct way to mark a function as server-only in Next.js?
Aimport server from 'next/server'
B'use server' at the top of the file
CAdd a comment // server-only
DUse <ServerOnly> component wrapper
What happens if you do NOT use 'use server' for a server-only function?
AThe function might be bundled and sent to the browser
BThe function runs only on the server anyway
CThe function will run twice on server and client
DNext.js throws a compile error
Can a 'use server' function use browser APIs like localStorage?
AYes, always
BOnly if wrapped in useEffect
CNo, it runs only on the server
DOnly in development mode
Where should the 'use server' directive be placed in a file?
AAt the very top before any code
BAfter imports
CAnywhere inside the function
DInside the component return
What is a benefit of using 'use server' in Next.js?
AImproves client-side rendering speed
BAllows server-only code to run in the browser
CAutomatically converts code to TypeScript
DReduces client bundle size and hides server logic
Explain what the 'use server' directive does in Next.js and why it is important.
Think about where your code runs and what you want to hide from the browser.
You got /3 concepts.
    Describe how to correctly use the 'use server' directive in a Next.js component or function.
    It's a special comment line that must come first.
    You got /3 concepts.