Skip to content

Feat: Extend custom response handling and passthrough request headers#40

Merged
alexdln merged 10 commits into
alexdln:mainfrom
onmotion:main
May 16, 2026
Merged

Feat: Extend custom response handling and passthrough request headers#40
alexdln merged 10 commits into
alexdln:mainfrom
onmotion:main

Conversation

@onmotion
Copy link
Copy Markdown
Contributor

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
@iamakulov
Copy link
Copy Markdown

+1 to this. nimpl-proxy-chain currently doesn’t work with next-intl’s locale routing because it seems to omit request headers set by the middlewares. This PR, I believe, would fix that.

@alexdln
Copy link
Copy Markdown
Owner

alexdln commented May 14, 2026

Thanks a lot for the PR and I apologize for the stale request @onmotion

I updated the logic that custom type returns final - implemented it via config option, as I think the original behavior has use cases

chain(middlewares, { breakOnTypes: ["redirect", "custom"] });

I also adjusted the data collection logic - if different middleware added cookies via the header, they could get lost. I think this is also related to the bug from @iamakulov

I've published it in the canary channel for now (1.1.0-canary.0). It would be great if you could check that everything works now. otherwise I'll publish the stable version [1.1.0] in two days

@alexdln alexdln merged commit 7248827 into alexdln:main May 16, 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.

3 participants