You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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.
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
changed the title
Feat: Fix custom response handling and passthrough request headers
Feat: Extend custom response handling and passthrough request headers
Feb 10, 2026
+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.
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
undefinedorNextResponse.next()there, then by default all request headers will be passed to the route. In thenimpl-proxy-chain, they are null.Response, it will be returned immediately "as is".Changes:
Passthrough Request Headers:
collectDatato initializerequestHeaderswith the original request headers (req.headers) instead of an empty object.formatResponseto includerequest.headersin theNextResponseconfiguration, ensuring modified headers are forwarded to Next.js.Custom Response Logic:
FINAL_SYMBOLincollectData.Examples: