Skip to content

Middleware: Setting new request headers breaks headers() #77597

@sommeeeer

Description

@sommeeeer

Link to the code that reproduces this issue

https://github.com/sommeeeer/next-middleware-headerslist-issue

To Reproduce

  1. pnpm build && pnpm start
  2. curl http://localhost:3000/issue
  3. go to http://localhost:3000/issue-page

Current vs. Expected behavior

I have taken the example code from https://nextjs.org/docs/app/building-your-application/routing/middleware#setting-headers and put it in my middleware.ts. This breaks headers() in any route:

You will get an error saying:

Image

I expect headers()to work, even though I have modified the request headers in middleware.ts

Provide environment information

Operating System:
  Platform: linux
  Arch: x64
  Version: #47~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Oct  2 16:16:55 UTC 2
  Available memory (MB): 14800
  Available CPU cores: 16
Binaries:
  Node: 20.18.0
  npm: 11.1.0
  Yarn: N/A
  pnpm: 9.15.4
Relevant Packages:
  next: 15.2.4 // Latest available version is detected (15.2.4).
  eslint-config-next: 15.2.4
  react: 19.1.0
  react-dom: 19.1.0
  typescript: 5.8.2
Next.js Config:
  output: N/A

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

Headers, Middleware

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

next dev (local), next build (local), next start (local)

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    HeadersRelated to the async headers() function.MiddlewareRelated to Next.js Middleware.locked

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions