Server and Client Components in Next.js · senior
Secrets, serialisation and environment poisoning
Only serializable data crosses from server to client, only NEXT_PUBLIC_ variables reach the browser (inlined at build time), and server-only code should be marked with import "server-only" and return small DTOs instead of whole database rows.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- Props to Client Components must be serializable by React: primitives, plain objects, arrays, Date, Map, Set, promises, JSX and Server Function references. Not class instances or ordinary functions.
- Fetch on the server and pass only what the UI needs (a DTO). Passing a whole user row can leak a password hash or phone number into the RSC payload.
- Only
NEXT_PUBLIC_*env vars are included in client bundles, and their values are inlined atnext build(frozen). Other vars are replaced with an empty string in client code. import "server-only"in a module makes importing it from a Client Component a build error;client-onlydoes the opposite.- A promise can be passed to a Client Component and unwrapped with
use()(the resolved value must be serializable). - React taint APIs (
taintUniqueValue,taintObjectReference) can block specific values or objects from being sent to the client, as an extra safety net. - Server Function return values are also serialized to the client: shape them like DTOs.
The red pen
Common traps: mistake → fix
Mistake: "Put the API key in
NEXT_PUBLIC_API_KEYso the client can call the API"Fix: it ships in the bundle; call the API from the server.
Mistake: "
NEXT_PUBLIC_values change when I change the env in the container"Fix: they were inlined at build time.
Mistake: "Client code can't see server data unless I render it"
Fix: props sent to client components are in the RSC payload even if not displayed.
Easy to hard
Questions interviewers ask, easy to hard
- A Client Component needs data from the database and a private API key. How do you structure it?
- What can and cannot be passed from a Server Component to a Client Component?
- What happens to
NEXT_PUBLIC_variables at build time? - What do
server-onlyand taint do?
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.