Skip to content

Conversation

@onmotion
Copy link

Fix custom response handling and passthrough request headers

Motivation:

I like this library, but I found that the behavior of middleware differs from that of NextJS.

This PR addresses two main issues:

  1. Request Headers Propagation: Previously, request headers were not being fully passed through or amendable within the middleware chain. For example, comparing with the behavior of nextjs proxy - if you return undefined or NextResponse.next() there, then by default all request headers will be passed to the route. In the nimpl-proxy-chain, they are null.
  2. Custom Response Handling: When a middleware returns a custom response (e.g., a base Response with status 401 for a basic auth), the chain needs to recognize this as a final state to prevent unintended processing or overwriting by subsequent logic. Compared to Next.js behavior, if return a basic Response, it will be returned immediately "as is".

Changes:

  • Passthrough Request Headers:

    • Updated collectData to initialize requestHeaders with the original request headers (req.headers) instead of an empty object.
    • Updated formatResponse to include request.headers in the NextResponse configuration, ensuring modified headers are forwarded to Next.js.
  • Custom Response Logic:

    • Added logic to mark custom responses (where a body is present) with FINAL_SYMBOL in collectData.
  • Examples:

    • Add the basic-auth example to demonstrate how to handle a custom Response within the chain.

@onmotion onmotion changed the title Feat: Fix custom response handling and passthrough request headers Feat: Extend custom response handling and passthrough request headers Feb 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant