Skip to main content
The next/headers module provides functions for accessing request headers and cookies in Server Components, Route Handlers, and Server Actions.

Import

headers

Returns a read-only Headers instance for the incoming request.

Return Type

Promise<Headers>
Standard Web Headers instance.Methods:
  • get(name: string): string | null
  • has(name: string): boolean
  • getSetCookie(): string[]
  • entries(): Iterator<[string, string]>
  • forEach(callback)
Read-only — mutations are ignored.

Usage Notes

Async in Next.js 15+: headers() returns a Promise. Always await it.
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 } | undefined
  • getAll(): Array<{ name: string; value: string }>
  • has(name: string): boolean
  • size: number
Write methods (Route Handlers / Server Actions only):
  • set(name: string, value: string, options?): this
  • set(options: { name: string; value: string; ... }): this
  • delete(name: string): this

Reading Cookies

Writing Cookies

Server Actions / Route Handlers only: Writing cookies in Server Components has no effect.

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 uses AsyncLocalStorage to maintain per-request context:
  1. Entry: Server entry sets context before rendering
  2. Access: headers() / cookies() read from ALS
  3. Cleanup: Context is cleared after response
This ensures concurrent requests don’t share state.

Dynamic Usage Tracking

Calling headers() or cookies() marks the page as dynamic:
  • ISR caching: Bypassed
  • Response headers: Cache-Control: no-store
  • Rendering: On-demand per request

Limitations

Client Components: Cannot call headers() or cookies() in client components. Pass data as props.
Writing in Server Components: Cookie writes in Server Components have no effect. Use Route Handlers or Server Actions.
SSR timing: These functions can only be called after the request context is set. Calling them at module-level or in global scope will throw.

Migration from Pages Router

Source

View source code → Implementation: /home/daytona/workspace/source/packages/vinext/src/shims/headers.ts