mirror of
https://github.com/cloudflare/vinext.git
synced 2026-05-09 08:25:34 +02:00
[PR #919] fix(middleware): align cookies and external rewrites #950
Labels
No labels
enhancement
enhancement
good first issue
help wanted
nextjs-tracking
nextjs-tracking
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
starred/vinext#950
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Original Pull Request: https://github.com/cloudflare/vinext/pull/919
State: closed
Merged: Yes
What this changes
Middleware cookie writes made through
NextResponse.cookies.set()are now mirrored intox-middleware-set-cookieand merged back into the request cookie store, socookies()can observe middleware-set values in the same render.External middleware rewrites now preserve the full absolute destination and proxy the request instead of collapsing the URL to
pathname + search. This covers App Router dev/prod, Pages Router generated entry handling, Pages production, deploy output, and the dev external proxy path.This also moves middleware matcher/execution/rewrite behavior out of generated entry strings and into normal runtime modules. Codegen now describes app shape and wires the user middleware module into shared helpers; it no longer owns matcher tokenization, middleware dispatch, waitUntil handling, internal-header filtering, or external middleware rewrite proxy behavior.
Why
Next.js mirrors middleware cookie writes into
x-middleware-set-cookieand later merges that internal header into the request store before rendering. Without that internal header, auth and session flows that set a cookie in middleware and read it in the same render observeundefined.Next.js also treats cross-origin middleware rewrites as external proxy destinations. vinext was stripping the origin from middleware rewrite URLs, which made
NextResponse.rewrite("https://api.example.com/...")route locally and often return a local 404.The codegen extraction is to prevent the exact class of drift this PR exposed: generated Pages/App entries had duplicated middleware filtering and rewrite logic that could diverge from the runtime path.
Next.js references
NextResponsemirrors cookie mutations intox-middleware-set-cookiemergeMiddlewareCookiesmergesx-middleware-set-cookieinto request cookies beforecookies()reads themresolve-routesapplies middleware request-header overrides and keepsx-middleware-set-cookieinternalresolve-routestreats protocol rewrite destinations as finished external rewritesrouter-serverproxies finished protocol destinations throughproxyRequestApproach
NextResponsecookie handling that syncsSet-Cookieintox-middleware-set-cookie.x-middleware-set-cookieinto the request cookie context while preserving existing request-header override behavior.proxyExternalRequest.x-middleware-*headers, includingx-middleware-set-cookie, from client responses and external upstream requests.server/middleware-matcher.ts,server/middleware-runtime.ts, andserver/app-middleware.tshelpers.Validation
vp test run tests/shims.test.ts -t "resolveMiddlewareHandler|matchPattern|matchesMiddleware|middleware bypass|runMiddleware"vp test run tests/entry-templates.test.tsrm -rf tests/fixtures/pages-basic/dist && vp test run tests/pages-router.test.ts -t "produces SSR server entry via vite build --ssr|runMiddleware preserves internal middleware cookie headers on rewrites|runMiddleware handles rewrite|runMiddleware preserves external middleware rewrite destinations|runMiddleware strips internal cookie headers from custom responses|runMiddleware handles redirects|runMiddleware passes waitUntil promises"vp test run tests/app-router.test.ts -t "external middleware rewrite|middleware rewrite|header override|x-vinext-mw-ctx|delegates middleware rewrite handling"vp checkvp run knip --no-progressvp run vinext#buildtests/entry-templates.test.ts,vp check --fix,knip --no-progress, and package rebuild.Risks / follow-ups
External middleware rewrites intentionally preserve the upstream proxy response status. That matches the Next.js external proxy path, where the middleware rewrite status is not carried into
proxyRequest.