Skip to content
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

fix: fixes cookie override during redirection from server action #61633

Merged
merged 6 commits into from
Apr 4, 2024
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
1 change: 1 addition & 0 deletions packages/next/src/server/lib/server-ipc/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const ipcForbiddenHeaders = [
export const actionsForbiddenHeaders = [
...ipcForbiddenHeaders,
'content-length',
'set-cookie',
technikhil314 marked this conversation as resolved.
Show resolved Hide resolved
]

export const filterReqHeaders = (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { redirect } from 'next/navigation'
import { cookies } from 'next/headers'

async function action() {
'use server'

cookies().set(
'custom-server-action-test-cookie',
'custom-server-action-test-cookie-val'
)
redirect('/another')
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ async function main() {
try {
const parsedUrl = parse(req.url, true)
const { pathname } = parsedUrl

res.setHeader(
'set-cookie',
'custom-server-test-cookie=custom-server-test-cookie-val'
)
if (pathname.startsWith('/base')) {
await handle(req, res, parsedUrl)
} else {
Expand Down
25 changes: 23 additions & 2 deletions test/e2e/app-dir/app-basepath-custom-server/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { join } from 'path'
import { createNextDescribe } from 'e2e-utils'
import { retry } from 'next-test-utils'
import { check, retry } from 'next-test-utils'
import { join } from 'path'

createNextDescribe(
'custom-app-server-action-redirect',
Expand Down Expand Up @@ -35,5 +35,26 @@ createNextDescribe(
// Count should still be 2 as the browser should not have reloaded the page.
expect(await getCount()).toBe('Count: 2')
})

it('redirects with proper cookies set from both redirect response and post respose', async () => {
const browser = await next.browser('/base')

await browser.elementById('submit-server-action-redirect').click()

expect(await browser.waitForElementByCss('#another').text()).toBe(
'Another Page'
)
expect(await browser.url()).toBe(
`http://localhost:${next.appPort}/base/another`
)
await check(
() => browser.eval('document.cookie'),
/custom-server-test-cookie/
)
await check(
() => browser.eval('document.cookie'),
/custom-server-action-test-cookie/
)
})
}
)
Loading