Skip to content

fix: remove internal next headers in middleware response #777

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

Merged
merged 4 commits into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/metal-news-watch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/aws": patch
---

fix: remove internal next headers in middleware response
7 changes: 7 additions & 0 deletions packages/open-next/src/core/routing/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ export async function handleMiddleware(
const resHeaders: Record<string, string | string[]> = {};

responseHeaders.delete("x-middleware-override-headers");
/* Next will set the header `x-middleware-set-cookie` when you `set-cookie` in the middleware.
* We can delete it here since it will be set in `set-cookie` aswell. Next removes this header in the response themselves.
* `x-middleware-next` is set when you invoke `NextResponse.next()`. We can delete it here aswell.
*/
responseHeaders.delete("x-middleware-set-cookie");
responseHeaders.delete("x-middleware-next");

const xMiddlewareKey = "x-middleware-request-";
responseHeaders.forEach((value, key) => {
if (key.startsWith(xMiddlewareKey)) {
Expand Down
Loading