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

Set multiple cookies on API routes (Pages Router) #64166

Open
nicolascavallin opened this issue Apr 7, 2024 · 4 comments
Open

Set multiple cookies on API routes (Pages Router) #64166

nicolascavallin opened this issue Apr 7, 2024 · 4 comments
Labels
bug Issue was opened via the bug report template. Runtime Related to Node.js or Edge Runtime with Next.js.

Comments

@nicolascavallin
Copy link

Link to the code that reproduces this issue

https://github.com/nicolascavallin/next-pages-firebase-auth-boilerplate/blob/main/src/pages/api/auth/session.ts

To Reproduce

## In API handler (Pages Router)

  • Set two cookies with res.setHeader("Set-Cookie": [cookie1, cookie2])

  • Then: if you are running local (pnpm dev and pnpm build + pnpm start), both cookies will be settled.

  • Deploy to Vercel: call the same API, only first cookie will be settled.

If changes the order of cookies, then again, first will be settled (in Vercel).

In API route (App Router)

  • Set two cookies with cookies()

  • Then: both cookies are settled in Vercel and in Local.

Current vs. Expected behavior

I expect to settled both cookies on Pages Router API.

Current (Vercel)

Screenshot 2024-04-07 at 12 24 30

Current (Local with HTTPS)

Screenshot 2024-04-07 at 12 25 05

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.4.0: Fri Mar 15 00:12:49 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T6020
Binaries:
  Node: 20.10.0
  npm: 10.2.3
  Yarn: 1.22.19
  pnpm: 8.12.1
Relevant Packages:
  next: 14.1.4
  eslint-config-next: 14.1.4
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.4.4
Next.js Config:
  output: N/A

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

Not sure, Middleware / Edge (API routes, runtime)

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

Vercel (Deployed)

Additional context

I suspect a lot of things and play with all properties: httpOnly, secure, sameSite, priority.

Also, suspecting a "big size" of data, I just try to set simple cookies like: foo1=bar1 and foo2=bar2.

Always getting the same result, only first cookie settled.

Also I tried

res.setHeader("Set-Cookie": cookie1)
res.setHeader("Set-Cookie": cookie2)
res.setHeader("Set-Cookie": `${cookie1}; ${cookie2}`)

And of course

res.setHeader("Set-Cookie": [cookie1, cookie2])

And nothing.

I tried to get help from cookies-next library... nothing.

I didn't found any solution and any similar issue, so I'm opening this one.

Thanks in advance.

@nicolascavallin nicolascavallin added the bug Issue was opened via the bug report template. label Apr 7, 2024
@github-actions github-actions bot added the Runtime Related to Node.js or Edge Runtime with Next.js. label Apr 7, 2024
@nicolascavallin
Copy link
Author

To bring more context:

I also tried with res.appendHeader

@therealgabryx
Copy link

appendHeader worked for me until yesterday, when we upgraded from "next": "^14.1.0" to "next": "^14.2.1".

I was successfully settings headers after login in a trpc. controller, by calling:

ctx.res.appendHeader("Set-Cookie", cookie_name1);
ctx.res.appendHeader("Set-Cookie", cookie_name2);

Since the update tho, the api stopped working and returning a 500 error, caused by the following:
Property 'appendHeader' does not exist on type 'NextApiResponse'.ts(2339)

This happened after upgrading the next package and simultaneously moving from npm to pnpm.

@therealgabryx
Copy link

therealgabryx commented Apr 18, 2024

Edit: upgrading to "next": "^14.2.2" (released 9hrs ago), fixed the typescript error on 'appendHeader'. The api error 500 still persists tho.

@nicolascavallin
Copy link
Author

@therealgabryx

Does it happens on local or in prod? Everything works for me in local, with pnpm, the issue is after deploy in Vercel

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

2 participants