-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Response headers don't stick when set across multiple chained middlewares #128
Comments
I've made that intentionally. There were 2 major issues with prev solution:
If you need to pass data across chain you should use context then. Current solution is way more stable and secure. I'm closing as it's not really an issue - but feel free to respond or reopen! All best! |
Out of curiosity, what were the bugs you were encountering with Actions that made this fix necessary? Currently, there doesn't seem to be any way to cleanly have multiple middlewares all contribute to the response headers. The code looks like it grabs any headers which were added to the response each middleware and collects them in a variable called nemo/packages/nemo/src/index.ts Lines 169 to 174 in a89dd65
Setting response headers, for example as required when implementing a CSP via middleware per Next's docs, must be performed in the final middleware in the chain or else they are lost. Furthermore, in order to set response headers at all, the middleware must return a response, it cannot be forwarded. Returning a response from any middleware halts execution of any subsequent middlewares, so the end result seems to be that the only real solution would be to have a dedicated middleware for composing a final response, which must be the final middleware to execute, and pulls together different headers using context. That's not necessarily a problem, but I don't think much of the above is made particularly clear in the docs currently. I'm happy to help out on either updating the docs or trying to implement a solution that allows response header composition without triggering either of the two problems mentioned here. Thanks for your work on this package, which helps solve a glaring omission from the core Next middleware implementation. |
I have two issues with this decision that led me to essentially making my own solution for this.
Anyway, agreed with @atothewest thanks for this lib. It was nice while it worked for us and at least inspired us to rethink how to do middleware in Next.js v13+ because the out of the box is definitely lacking. |
I'm preparing major update which will improve headers handling and include revert to 6.1 version of path-to-regexp dep for more usability and to match next.js native dep version. Thanks for feedback! |
Would be solved in v2 (#140) |
Hello @derekaug, @atothewest - could I ask you to check the current WIP v2 version in #140? bun add https://pkg.pr.new/z4nr34l/nemo/@rescale/nemo@140 Feedback on that stage would be helpful and will allow me to prepare better docs or event change direction of that solution. Thanks in advance. |
Thanks for the update @z4nr34l I will check it out as soon as I can this week 🙏 |
Describe the bug
When setting response headers in one middleware and forwarding the response, the response headers are not being applied further down the chain. The only headers "that stick" are headers set within a middleware that you return a
NextResponse
on.To Reproduce
Steps to reproduce the behavior:
Expected behavior
I expect each middleware to get the same response and have each of their headers applied to it, so a log output like:
Screenshots
If applicable, add screenshots to help explain your problem.
Additional context
Add any other context about the problem here.
This was working in versions
1.2.*
at least in the case that I grabbed the response from the arguments passed in and returnedNextResponse.next()
. Middlewares further down the chain would havex-some-header
set and applied in their responses (this was in abefore
middleware).https://stackblitz.com/edit/stackblitz-starters-z96vrexv?file=middleware.ts
The text was updated successfully, but these errors were encountered: