Skip to content

Commit

Permalink
feat: add multiple set-cookie headers in middleware (#1970)
Browse files Browse the repository at this point in the history
* feat: add multiple set-cookie headers in middleware

* chore: format

* chore: change string order

* chore: version
  • Loading branch information
sarahetter committed Mar 7, 2023
1 parent c386385 commit 32b31c6
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 8 deletions.
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

0 comments on commit 32b31c6

Please sign in to comment.