Skip to content

Commit 878ddd7

Browse files
taty2010ericapisaniascorbickodiakhq[bot]
authored
fix: merge Middleware and API route response cookies (#1870)
* fix: appending origin cookies to prevent override and to match test order * test: remove usuallySkip from tests related to middleware + API cookie merge * Fix: let to const Co-authored-by: Erica Pisani <[email protected]> * change to strict comparison Co-authored-by: Matt Kane <[email protected]> Co-authored-by: Erica Pisani <[email protected]> Co-authored-by: Matt Kane <[email protected]> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent 49756be commit 878ddd7

File tree

2 files changed

+7
-4
lines changed
  • packages/runtime/src/templates/edge-shared
  • test/e2e/modified-tests/skip-trailing-slash-redirect

2 files changed

+7
-4
lines changed

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

+5
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,13 @@ 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')
3435
middlewareResponse.headers.forEach((value, key) => {
3536
response.headers.set(key, value)
37+
// Append origin cookies after middleware cookies
38+
if (key === 'set-cookie' && originCookies) {
39+
response.headers.append(key, originCookies)
40+
}
3641
})
3742
return response
3843
}

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

+2-4
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,14 @@ describe('skip-trailing-slash-redirect', () => {
4747
expect(res.headers.get('x-from-middleware')).toBe('true')
4848
expect(await res.text()).toBe('hello from middleware')
4949
})
50-
// NTL Skip
51-
usuallySkip('should merge cookies from middleware and API routes correctly', async () => {
50+
it('should merge cookies from middleware and API routes correctly', async () => {
5251
const res = await fetchViaHTTP(next.url, '/api/test-cookie', undefined, {
5352
redirect: 'manual',
5453
})
5554
expect(res.status).toBe(200)
5655
expect(res.headers.get('set-cookie')).toEqual('from-middleware=1; Path=/, hello=From API')
5756
})
58-
// NTL Skip
59-
usuallySkip('should merge cookies from middleware and edge API routes correctly', async () => {
57+
it('should merge cookies from middleware and edge API routes correctly', async () => {
6058
const res = await fetchViaHTTP(next.url, '/api/test-cookie-edge', undefined, {
6159
redirect: 'manual',
6260
})

0 commit comments

Comments
 (0)