diff --git a/packages/runtime/src/templates/edge-shared/utils.ts b/packages/runtime/src/templates/edge-shared/utils.ts index 1dc9153cc8..98980c1b7b 100644 --- a/packages/runtime/src/templates/edge-shared/utils.ts +++ b/packages/runtime/src/templates/edge-shared/utils.ts @@ -31,8 +31,13 @@ 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) + } }) return response } diff --git a/test/e2e/modified-tests/skip-trailing-slash-redirect/index.test.ts b/test/e2e/modified-tests/skip-trailing-slash-redirect/index.test.ts index 937b9e2a63..c9d60ce5a7 100644 --- a/test/e2e/modified-tests/skip-trailing-slash-redirect/index.test.ts +++ b/test/e2e/modified-tests/skip-trailing-slash-redirect/index.test.ts @@ -47,16 +47,14 @@ describe('skip-trailing-slash-redirect', () => { expect(res.headers.get('x-from-middleware')).toBe('true') expect(await res.text()).toBe('hello from middleware') }) - // NTL Skip - usuallySkip('should merge cookies from middleware and API routes correctly', async () => { + it('should merge cookies from middleware and API routes correctly', async () => { const res = await fetchViaHTTP(next.url, '/api/test-cookie', undefined, { redirect: 'manual', }) expect(res.status).toBe(200) expect(res.headers.get('set-cookie')).toEqual('from-middleware=1; Path=/, hello=From API') }) - // NTL Skip - usuallySkip('should merge cookies from middleware and edge API routes correctly', async () => { + 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', })