Skip to content

Commit 32b31c6

Browse files
authored
feat: add multiple set-cookie headers in middleware (#1970)
* feat: add multiple set-cookie headers in middleware * chore: format * chore: change string order * chore: version
1 parent c386385 commit 32b31c6

File tree

3 files changed

+7
-8
lines changed

3 files changed

+7
-8
lines changed

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/runtime/src/templates/edge-shared/utils.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,11 @@ export const addMiddlewareHeaders = async (
3131
// We need to await the response to get the origin headers, then we can add the ones from middleware.
3232
const res = await originResponse
3333
const response = new Response(res.body, res)
34-
const originCookies = response.headers.get('set-cookie')
3534
middlewareResponse.headers.forEach((value, key) => {
36-
response.headers.set(key, value)
37-
// Append origin cookies after middleware cookies
38-
if (key === 'set-cookie' && originCookies) {
39-
response.headers.append(key, originCookies)
35+
if (key === 'set-cookie') {
36+
response.headers.append(key, value)
37+
} else {
38+
response.headers.set(key, value)
4039
}
4140
})
4241
return response

test/e2e/modified-tests/skip-trailing-slash-redirect/index.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ describe('skip-trailing-slash-redirect', () => {
5252
redirect: 'manual',
5353
})
5454
expect(res.status).toBe(200)
55-
expect(res.headers.get('set-cookie')).toEqual('from-middleware=1; Path=/, hello=From API')
55+
expect(res.headers.get('set-cookie')).toEqual('hello=From API, from-middleware=1; Path=/')
5656
})
5757
it('should merge cookies from middleware and edge API routes correctly', async () => {
5858
const res = await fetchViaHTTP(next.url, '/api/test-cookie-edge', undefined, {
5959
redirect: 'manual',
6060
})
6161
expect(res.status).toBe(200)
62-
expect(res.headers.get('set-cookie')).toEqual('from-middleware=1; Path=/, hello=From%20API; Path=/')
62+
expect(res.headers.get('set-cookie')).toEqual('hello=From%20API; Path=/, from-middleware=1; Path=/')
6363
})
6464

6565
if ((global as any).isNextStart) {

0 commit comments

Comments
 (0)