next.js icon indicating copy to clipboard operation
next.js copied to clipboard

cookies on behaving as expected on API routes

Open professorhaseeb opened this issue 1 year ago • 4 comments

Verify canary release

  • [X] I verified that the issue exists in the latest Next.js canary release

Provide environment information

    Operating System:
      Platform: win32
      Arch: x64
      Version: Windows 10 Pro
    Binaries:
      Node: 18.12.0
      npm: N/A
      Yarn: N/A
      pnpm: N/A
    Relevant packages:
      next: 13.0.1-canary.4
      eslint-config-next: 13.0.0
      react: 18.2.0
      react-dom: 18.2.0

What browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

Describe the Bug

You're importing a component that needs next/headers. That only works in a Server Component but one of its parents is marked with "use client", so it's a Client Component.

   ,----
 3 | import { cookies } from 'next/headers';
   : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   `----

One of these is marked as a client entry with "use client":
pages\api\hello.tsx

Expected Behavior

probably return cookes as expected.

Link to reproduction

no link

To Reproduce

//pages/api/hello.tsx

import { cookies } from 'next/headers';

export default async function handler(req: any, res: any) {
    console.log(cookies())
}

professorhaseeb avatar Nov 01 '22 11:11 professorhaseeb

note: there is no "use client"; on my code

professorhaseeb avatar Nov 01 '22 11:11 professorhaseeb

I'm also seeing this bug. I have no "use client" in my code.

sourcecodes2 avatar Nov 02 '22 12:11 sourcecodes2

Same

filippofinke avatar Nov 04 '22 14:11 filippofinke

I think for api routes, you should change the extension from .tsx to .ts because nextjs will interpret your file as a react component, whereas it is an api route.

Fredkiss3 avatar Nov 04 '22 16:11 Fredkiss3