cookies-next icon indicating copy to clipboard operation
cookies-next copied to clipboard

Error when invoking `setCookie` on a new `Request` where `cookies` is `undefined`

Open JonasDoe opened this issue 7 months ago • 0 comments

If I create a new (fetch API) Request in a Next.js Route Handlers, e.g.

export async function GET(req: Request) {
  const myResponse = new Response('Hello, World', { status: 200, statusText: 'OK' });
  setCookie('myKey', 'myValue', { req, res: myResponse });
  return myResponse
}

this line of code in thecookies-next library will fail b/c res.cookies is undefined:

if (res) {
    res.cookies.set(payload); //  TypeError: Cannot read properties of undefined (reading 'set')
}

Curiously enough, there's no error if I just invoke setCookie('myKey', 'myValue', { res: myResponse }); (i.e. without res). But the Response won't bear any Set-Cookie entries then. Maybe I'm just misunderstanding the behavior of the library here?

JonasDoe avatar Jul 10 '24 11:07 JonasDoe