Skip to content

fix: cookies set in middleware accessible during the same request #2847

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 9 commits into from
Apr 18, 2025
Prev Previous commit
Next Next commit
fix: make cookies available during first request on rewrite as well
  • Loading branch information
mrstork committed Apr 17, 2025
commit 4a900a78feb5e5e2f26ba3f3182729deff397209
10 changes: 9 additions & 1 deletion edge-runtime/lib/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,15 @@ export const buildResponse = async ({
}
edgeResponse.headers.set('x-middleware-rewrite', relativeUrl)
request.headers.set('x-middleware-rewrite', target)
return addMiddlewareHeaders(context.rewrite(target), edgeResponse)

// coookies set in middleware need to be available during the lambda request
const newRequest = new Request(target, request)
const newRequestCookies = mergeMiddlewareCookies(edgeResponse, newRequest)
if (newRequestCookies) {
newRequest.headers.set('Cookie', newRequestCookies)
}

return addMiddlewareHeaders(context.next(newRequest), edgeResponse)
}

if (redirect) {
Expand Down
Loading