Async cookies and headers
What you need to know
cookies() and headers() from next/headers expose request information to server code. In modern Next.js they are asynchronous; await the returned value before reading it. Use a cookie for a harmless preference and a header only as a request-supplied hint. Neither is trusted identity simply because the server can read it.
By the end, you can: read request context; distinguish mutable contexts.
app/preferences/page.tsx
import { cookies, headers } from "next/headers";export default async function Preferences() { const store = await cookies(); const requested = store.get("view")?.value; const view = requested === "compact" ? "compact" : "comfortable"; const language = (await headers()).get("accept-language") ? "Language hint present" : "No language hint"; return <main><h1>Preferences</h1><p>View: {view}</p><p>{language}</p></main>;}Expected behavior (not framework-tested): A view=compact cookie selects compact; missing or other values use comfortable. A header is acknowledged without reflecting raw input.
How it works
cookies().get after awaiting returns a cookie entry or undefined; its value is a string. headers() returns a read-only Headers interface, and get returns a string or null. Read APIs can be used in Server Components; cookie writes and deletes need a supported Server Function or Route Handler response context, before HTTP headers have been sent. This batch previews a mutation signature but does not implement a public endpoint. A browser cannot directly read an HttpOnly cookie through document.cookie, though the server can receive it. A preference cookie is not an authenticated user session.
Cookie mutation preview (server function, not wired to a form)
"use server";import { cookies } from "next/headers";export async function setViewPreference(value: unknown) { if (value !== "compact" && value !== "comfortable") throw new Error("Invalid preference"); (await cookies()).set("view", value, { path: "/", sameSite: "lax", httpOnly: true, secure: true });}Expected behavior (not framework-tested): In a supported HTTPS mutation context this sets a bounded preference cookie. Local plain HTTP behavior depends on browser rules; this preview is not exercised by the fixture.
Common mistake
Do not call cookies().get without await in a Next.js 16 lesson. Do not call set while a Server Component is rendering or attempt to mutate the object from headers(). Validate values and use real authenticated sessions for sensitive access, not forwarded headers from unknown clients.
Practice and verification
Test two isolated requests with different view cookies and a third without one. Check each displayed preference. Then explain why a cookie update must be performed in a response-capable context instead of during page rendering.
Scope: App Router, proposed Next.js 16.x, Cache Components disabled for these labs. Snippets assume a reviewed TypeScript app and are static examples; framework behavior has not been executed. Exact supported patch and dependency set must be verified before running.
Further reading: Cookies · Headers · Async Apis · Data Security