Skip to content

feat: add multiple set-cookie headers in middleware #1970

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 6 commits into from
Mar 7, 2023
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
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions packages/runtime/src/templates/edge-shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,11 @@ export const addMiddlewareHeaders = async (
// We need to await the response to get the origin headers, then we can add the ones from middleware.
const res = await originResponse
const response = new Response(res.body, res)
const originCookies = response.headers.get('set-cookie')
middlewareResponse.headers.forEach((value, key) => {
response.headers.set(key, value)
// Append origin cookies after middleware cookies
if (key === 'set-cookie' && originCookies) {
response.headers.append(key, originCookies)
if (key === 'set-cookie') {
response.headers.append(key, value)
} else {
response.headers.set(key, value)
}
})
return response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ describe('skip-trailing-slash-redirect', () => {
redirect: 'manual',
})
expect(res.status).toBe(200)
expect(res.headers.get('set-cookie')).toEqual('from-middleware=1; Path=/, hello=From API')
expect(res.headers.get('set-cookie')).toEqual('hello=From API, from-middleware=1; Path=/')
})
it('should merge cookies from middleware and edge API routes correctly', async () => {
const res = await fetchViaHTTP(next.url, '/api/test-cookie-edge', undefined, {
redirect: 'manual',
})
expect(res.status).toBe(200)
expect(res.headers.get('set-cookie')).toEqual('from-middleware=1; Path=/, hello=From%20API; Path=/')
expect(res.headers.get('set-cookie')).toEqual('hello=From%20API; Path=/, from-middleware=1; Path=/')
})

if ((global as any).isNextStart) {
Expand Down