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

cookies().set inside Server Action is broken when using middleware - dev mode only #63467

Open
padupuy opened this issue Mar 19, 2024 · 3 comments
Labels
bug Issue was opened via the bug report template. Runtime Related to Node.js or Edge Runtime with Next.js.

Comments

@padupuy
Copy link

padupuy commented Mar 19, 2024

Link to the code that reproduces this issue

https://github.com/padupuy/nextjs-server-action-cookies-middleware-bug

To Reproduce

In dev mode :

  1. Create a middleware.ts and attach a cookie to the NextResponse
  2. Create a server action, create a cookie and redirect the user
nextjs-bug-server-action-cookie-middleware.mp4

Current vs. Expected behavior

In dev mode :
I would like to set a cookie A from Server Action and a cookie B from the middleware.ts.
I found that if only set a cookie A from Server Action without doing anything into middleware.ts it works well.
Unfortunately, if I also set a cookie B inside the middleware, only the cookie B from the middleware is sent to the browser.
I expect that both cookie A and B are sent to the browser

In the live example, the bug is not present : https://nextjs-server-action-cookies-middleware-bug.vercel.app/

Provide environment information

Operating System:
  Platform: darwin
  Arch: x64
  Version: Darwin Kernel Version 23.3.0: Wed Dec 20 21:28:58 PST 2023; root:xnu-10002.81.5~7/RELEASE_X86_64
Binaries:
  Node: 20.9.0
  npm: 10.1.0
  Yarn: 1.22.19
  pnpm: N/A
Relevant Packages:
  next: 14.1.3
  eslint-config-next: 14.1.3
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.4.2
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Middleware / Edge (API routes, runtime)

Which stage(s) are affected? (Select all that apply)

next dev (local)

Additional context

No response

@padupuy padupuy added the bug Issue was opened via the bug report template. label Mar 19, 2024
@github-actions github-actions bot added the Runtime Related to Node.js or Edge Runtime with Next.js. label Mar 19, 2024
@devjiwonchoi
Copy link
Member

I'm assuming this is an expected behavior of redirect since it will replace the current request including the added Set-Cookie header.

@padupuy
Copy link
Author

padupuy commented Mar 25, 2024

This issue is maybe related to #49442

@marcospassos
Copy link

Any cookie set on a server action is overridden if a cookie is set on the middleware. Additionally, any cookies set on server actions are not available in the response.cookies

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template. Runtime Related to Node.js or Edge Runtime with Next.js.
Projects
None yet
Development

No branches or pull requests

3 participants