vercel / vercel/next.js

Docs: Composing middleware that forward request headers via `NextResponse.next({ request: { headers } })`

Open
#88,730 1 comment 2 reactions 1 assignee View on GitHub

@icyJoseph is already working on this.

Since Feb 5, 2026.

Dominant language
JavaScript
Stars
142k
Forks
32.4k
Avg merge
2d 14h
Merged PRs (30d)
351

Description

What is the documentation issue?

The Middleware documentation explains how to modify request headers using NextResponse.next({ request: { headers } }), but it doesn't cover how to compose multiple middleware that each need to forward request headers.

When using multiple middleware (e.g., authentication + i18n), each may need to forward internal request headers:

  • Auth middleware forwards x-session-id, x-user-id
  • i18n middleware forwards x-locale

There's no documented pattern for merging these forwarded headers. The current examples show single middleware only. This leads developers to either:

  1. Mutate request.headers directly (not documented/supported)
  2. Parse internal x-middleware-* headers (brittle)
  3. Give up on composition

The docs should include a recommended pattern for this common use case.

Is there any context that might help us understand?

Example use case:

// auth-middleware.ts
export function withAuth(request) {
  const headers = new Headers(request.headers);
  headers.set('x-session-id', 'abc');
  return NextResponse.next({ request: { headers } });
}

// i18n-middleware.ts  
export function withLocale(request) {
  const headers = new Headers(request.headers);
  headers.set('x-locale', 'en');
  return NextResponse.next({ request: { headers } });
}

// middleware.ts - How to compose these?
export function middleware(request) {
  const authRes = withAuth(request);
  const i18nRes = withLocale(request);
  
  // ❌ No documented way to merge forwarded request headers
  // Currently must choose: authRes OR i18nRes, not both
}

Current workarounds:

  • Mutating request.headers before calling other middleware (works but not documented/guaranteed)
  • Parsing internal x-middleware-override-headers (brittle, depends on implementation details)

Why this matters:

  • Middleware composition is essential for real apps
  • Auth + i18n is a classic combination
  • The lack of a pattern pushes complexity to userland
  • Library authors (AuthKit, next-intl, etc.) can't provide clean composition helpers

Requested addition:
Document a recommended pattern for merging forwarded request headers, or note that this is a current limitation and suggest the least-brittle workaround.

Does the docs page already exist? Please link to it.

https://nextjs.org/docs/app/building-your-application/routing/middleware#setting-headers

The "Setting Headers" section shows the basic usage but doesn't address composition.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.