nextjs - 💡(How to fix) Fix Docs: Composing middleware that forward request headers via `NextResponse.next({ request: { headers } })` [1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
vercel/next.js#88730Fetched 2026-04-08 02:04:04
View on GitHub
Comments
1
Participants
2
Timeline
7
Reactions
2
Author
Participants
Assignees
Timeline (top)
subscribed ×2assigned ×1commented ×1cross-referenced ×1

Fix Action

Fix / Workaround

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)

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.

Code Example

// 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
}
RAW_BUFFERClick to expand / collapse

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.

extent analysis

TL;DR

To compose multiple middleware that forward request headers, a recommended pattern for merging these headers should be documented or a workaround such as creating a utility function to merge headers from multiple middleware responses should be suggested.

Guidance

  • Identify the specific headers that need to be forwarded by each middleware and create a utility function to merge these headers.
  • Consider using a library or utility that can handle header merging to avoid brittle implementation details.
  • Evaluate the current workarounds (mutating request.headers or parsing internal headers) for their feasibility and potential risks in your specific use case.
  • If possible, suggest a documentation update to include a recommended pattern for merging forwarded request headers in the Next.js middleware documentation.

Example

// header-merger.ts
export function mergeHeaders(...responses: NextResponse[]) {
  const mergedHeaders = new Headers();
  responses.forEach((response) => {
    response.headers.forEach((value, key) => {
      mergedHeaders.set(key, value);
    });
  });
  return mergedHeaders;
}

// middleware.ts
export function middleware(request) {
  const authRes = withAuth(request);
  const i18nRes = withLocale(request);
  const mergedHeaders = mergeHeaders(authRes, i18nRes);
  return NextResponse.next({ request: { headers: mergedHeaders } });
}

Notes

The provided example assumes that NextResponse objects have a headers property that can be accessed and merged. This might need adjustments based on the actual implementation of NextResponse and the requirements of your application.

Recommendation

Apply a workaround such as creating a utility function to merge headers from multiple middleware responses, as documenting a recommended pattern might require updates to the Next.js documentation or core functionality.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING