Question presented to candidate: "A Server Component needs to know the user's theme cookie and preferred language from the request headers. Show how you read them in the App Router, then explain how you would let the user change the theme, and what reading them does to how the page is rendered."
What a strong answer should cover:
cookies()andheaders()are imported fromnext/headers, are async (awaitthem; sync access was removed in Next.js 16) and work in Server Components, Server Actions and Route Handlers β not in Client Components.cookies()returns a store withget(an object withnameandvalue),getAll,has, andset/delete;headers()returns a read-only WebHeadersobject.- Cookies can only be set or deleted in a Server Action or Route Handler, because the response headers are already sent once rendering streams;
headers()can never be modified. - They are request-time APIs: using them makes the route dynamic, and with Cache Components they must sit inside
<Suspense>or the build fails. - Security: set auth cookies with
httpOnly,secure,sameSite; never trust header values blindly.
Clarifying questions expected:
- "Does the rest of the page have to be per-user, or only this part?" β decides where the Suspense boundary goes.
- "Does client JavaScript need to read the cookie?" β if not, make it
httpOnly.
Code / implementation expected: Yes β reading in a Server Component and setting in a Server Action.