next/headers module provides functions for accessing request headers and cookies in Server Components, Route Handlers, and Server Actions.
Import
headers
Returns a read-onlyHeaders instance for the incoming request.
Return Type
Promise<Headers>
Standard Web
Headers instance.Methods:get(name: string): string | nullhas(name: string): booleangetSetCookie(): string[]entries(): Iterator<[string, string]>forEach(callback)
Usage Notes
Dynamic rendering: Calling
headers() opts the page out of static rendering and ISR caching.Example: Route Handler
cookies
Returns a cookie accessor for reading and writing cookies.Return Type
Promise<RequestCookies>
Cookie accessor with read/write methods.Read methods:
get(name: string): { name: string; value: string } | undefinedgetAll(): Array<{ name: string; value: string }>has(name: string): booleansize: number
set(name: string, value: string, options?): thisset(options: { name: string; value: string; ... }): thisdelete(name: string): this
Reading Cookies
Writing Cookies
Cookie Options
draftMode
Enable/disable draft mode for preview deployments.Return Type
Promise<DraftModeResult>
Draft mode controller.
Usage: Enable Draft Mode
Usage: Disable Draft Mode
Usage: Check Draft Mode
How It Works
Request Context
vinext usesAsyncLocalStorage to maintain per-request context:
- Entry: Server entry sets context before rendering
- Access:
headers()/cookies()read from ALS - Cleanup: Context is cleared after response
Dynamic Usage Tracking
Callingheaders() or cookies() marks the page as dynamic:
- ISR caching: Bypassed
- Response headers:
Cache-Control: no-store - Rendering: On-demand per request
Limitations
Migration from Pages Router
Source
View source code → Implementation:/home/daytona/workspace/source/packages/vinext/src/shims/headers.ts