nextjs - 💡(How to fix) Fix Cannot Set Vary Header in Middleware Since v15.5.0 [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#85851Fetched 2026-04-08 02:13:37
View on GitHub
Comments
1
Participants
2
Timeline
6
Reactions
0
Timeline (top)
labeled ×2closed ×1commented ×1issue_type_added ×1

Root Cause

But the response does not include the custom vary header, because it gets overridden by Next.js.

Code Example

response.headers.append('my-custom-header', 'test');
response.headers.append('vary', 'my-custom-header');

---

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 24.6.0: Mon Aug 11 21:16:34 PDT 2025; root:xnu-11417.140.69.701.11~1/RELEASE_ARM64_T6020
  Available memory (MB): 16384
  Available CPU cores: 12
Binaries:
  Node: 22.20.0
  npm: 10.9.3
  Yarn: N/A
  pnpm: 10.20.0
Relevant Packages:
  next: 16.0.2-canary.9 // Latest available version is detected (16.0.2-canary.9).
  eslint-config-next: N/A
  react: 19.2.0
  react-dom: 19.2.0
  typescript: 5.9.3
Next.js Config:
  output: N/A
RAW_BUFFERClick to expand / collapse

Link to the code that reproduces this issue

https://codesandbox.io/p/devbox/elated-swanson-d65gkj

To Reproduce

  1. Clone reproduction repository
  2. Install Dependencies (npm i)
  3. Start server (npm run dev)
  4. Open http://localhost:3000/ in your browser
  5. Open your browsers dev tools and inspect the request in the network tab (might need to reload the page if dev tools was opened after the page load)
  6. Check headers

Current vs. Expected behavior

The middleware in the reproduction example sets two headers, my-custom-header and vary.

response.headers.append('my-custom-header', 'test');
response.headers.append('vary', 'my-custom-header');

The my-custom-header header is correctly included in the response.

But the response does not include the custom vary header, because it gets overridden by Next.js.

It should be possible to add a custom vary header, which then gets merged with the default Next.js header.

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 24.6.0: Mon Aug 11 21:16:34 PDT 2025; root:xnu-11417.140.69.701.11~1/RELEASE_ARM64_T6020
  Available memory (MB): 16384
  Available CPU cores: 12
Binaries:
  Node: 22.20.0
  npm: 10.9.3
  Yarn: N/A
  pnpm: 10.20.0
Relevant Packages:
  next: 16.0.2-canary.9 // Latest available version is detected (16.0.2-canary.9).
  eslint-config-next: N/A
  react: 19.2.0
  react-dom: 19.2.0
  typescript: 5.9.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Middleware

Which stage(s) are affected? (Select all that apply)

next build (local), next start (local), Vercel (Deployed), Other (Deployed)

Additional context

This is the same issue as #48480. It was fixed in 15.2.0 but reappeared in 15.5.0.

extent analysis

TL;DR

  • The issue can be worked around by manually merging the custom vary header with the default Next.js header in the middleware.

Guidance

  • Review the Next.js documentation for middleware to understand how headers are handled and overridden.
  • Check the response.headers object for any existing vary headers before appending a custom one.
  • Consider using a different approach to set custom headers, such as using the response.setHeader method instead of append.
  • Verify that the custom vary header is correctly merged with the default Next.js header by inspecting the response headers in the browser's dev tools.

Example

const customVaryHeader = 'my-custom-header';
const existingVaryHeader = response.headers.get('vary');
if (existingVaryHeader) {
  response.headers.set('vary', `${existingVaryHeader}, ${customVaryHeader}`);
} else {
  response.headers.set('vary', customVaryHeader);
}

Notes

  • The issue seems to be related to a regression in Next.js version 15.5.0, which was previously fixed in version 15.2.0.
  • The provided example code only sets two headers, my-custom-header and vary, but the actual implementation may involve more complex header management.

Recommendation

  • Apply workaround: Manually merge the custom vary header with the default Next.js header in the middleware, as shown in the example code snippet. This approach allows for more control over the headers and can help resolve the issue until a fix is available in a future version of Next.js.

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