Skip to content

fix: merge Middleware and API route response cookies #1870

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 5 commits into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions packages/runtime/src/templates/edge-shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
})
Expand Down