[PR #365] [MERGED] fix: preserve middleware redirect headers in production #511

Closed
opened 2026-05-06 13:08:28 +02:00 by BreizhHardware · 0 comments

📋 Pull Request Information

Original PR: https://github.com/cloudflare/vinext/pull/365
Author: @NathanDrake2406
Created: 3/9/2026
Status: Merged
Merged: 3/9/2026
Merged by: @james-elicx

Base: mainHead: fix/middleware-redirect-headers


📝 Commits (3)

  • e1415ff fix: preserve middleware redirect headers (Set-Cookie) in production
  • 23c8c5a test: update entry template snapshot for redirect header fix
  • b68f76c Merge remote-tracking branch 'origin/main' into fix/middleware-redirect-headers

📊 Changes

7 files changed (+87 additions, -5 deletions)

View changed files

📝 packages/vinext/src/deploy.ts (+7 -1)
📝 packages/vinext/src/index.ts (+7 -1)
📝 packages/vinext/src/server/prod-server.ts (+15 -2)
📝 tests/__snapshots__/entry-templates.test.ts.snap (+7 -1)
📝 tests/deploy.test.ts (+11 -0)
📝 tests/fixtures/pages-basic/middleware.ts (+8 -0)
📝 tests/pages-router.test.ts (+32 -0)

📄 Description

Summary

  • Middleware Set-Cookie headers (and other non-internal headers) on redirect responses were silently dropped in production while working correctly in dev
  • Fixed in three locations: prod-server.ts (Pages Router prod), deploy.ts (Workers entry), index.ts (inline runMiddleware codegen)
  • The dev server at index.ts:2934 already handled this correctly — this was a dev/prod parity bug

Root cause

middleware.ts correctly collects non-internal headers on redirect responses into responseHeaders, but the three production consumers ignored it:

File Issue
prod-server.ts:814 res.writeHead() only sent Location
deploy.ts:600 new Response() only had { Location }
index.ts:786 Inline codegen didn't collect headers for redirects (but did for next() and rewrite())

Test plan

  • tests/deploy.test.ts — verifies generated worker entry references responseHeaders in redirect branch
  • tests/pages-router.test.ts — built entry runMiddleware returns responseHeaders with cookies on redirect
  • tests/pages-router.test.ts — prod server integration: Set-Cookie headers survive middleware redirect response
  • All existing middleware tests pass (28 production build + server middleware tests, 10 deploy middleware tests)

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/cloudflare/vinext/pull/365 **Author:** [@NathanDrake2406](https://github.com/NathanDrake2406) **Created:** 3/9/2026 **Status:** ✅ Merged **Merged:** 3/9/2026 **Merged by:** [@james-elicx](https://github.com/james-elicx) **Base:** `main` ← **Head:** `fix/middleware-redirect-headers` --- ### 📝 Commits (3) - [`e1415ff`](https://github.com/cloudflare/vinext/commit/e1415ffeb433de2341197333d20ea943d2afcb92) fix: preserve middleware redirect headers (Set-Cookie) in production - [`23c8c5a`](https://github.com/cloudflare/vinext/commit/23c8c5a126e5fb499497c721dd9376da997aca1c) test: update entry template snapshot for redirect header fix - [`b68f76c`](https://github.com/cloudflare/vinext/commit/b68f76c28d37fe96f8b627dd088c8c4d8f1e09c7) Merge remote-tracking branch 'origin/main' into fix/middleware-redirect-headers ### 📊 Changes **7 files changed** (+87 additions, -5 deletions) <details> <summary>View changed files</summary> 📝 `packages/vinext/src/deploy.ts` (+7 -1) 📝 `packages/vinext/src/index.ts` (+7 -1) 📝 `packages/vinext/src/server/prod-server.ts` (+15 -2) 📝 `tests/__snapshots__/entry-templates.test.ts.snap` (+7 -1) 📝 `tests/deploy.test.ts` (+11 -0) 📝 `tests/fixtures/pages-basic/middleware.ts` (+8 -0) 📝 `tests/pages-router.test.ts` (+32 -0) </details> ### 📄 Description ## Summary - Middleware `Set-Cookie` headers (and other non-internal headers) on redirect responses were silently dropped in production while working correctly in dev - Fixed in three locations: `prod-server.ts` (Pages Router prod), `deploy.ts` (Workers entry), `index.ts` (inline `runMiddleware` codegen) - The dev server at `index.ts:2934` already handled this correctly — this was a dev/prod parity bug ### Root cause `middleware.ts` correctly collects non-internal headers on redirect responses into `responseHeaders`, but the three production consumers ignored it: | File | Issue | |------|-------| | `prod-server.ts:814` | `res.writeHead()` only sent `Location` | | `deploy.ts:600` | `new Response()` only had `{ Location }` | | `index.ts:786` | Inline codegen didn't collect headers for redirects (but did for `next()` and `rewrite()`) | ## Test plan - [x] `tests/deploy.test.ts` — verifies generated worker entry references `responseHeaders` in redirect branch - [x] `tests/pages-router.test.ts` — built entry `runMiddleware` returns `responseHeaders` with cookies on redirect - [x] `tests/pages-router.test.ts` — prod server integration: `Set-Cookie` headers survive middleware redirect response - [x] All existing middleware tests pass (28 production build + server middleware tests, 10 deploy middleware tests) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
BreizhHardware 2026-05-06 13:08:28 +02:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/vinext#511
No description provided.