How to get cookie in client and server components #57564
Replies: 2 comments 8 replies
-
Right, so you mean, that this createService function must be invoked from a client component? Because if not, you have to structure your imports/exports, so that this function is never included in client bundles. |
Beta Was this translation helpful? Give feedback.
-
You should check for middleware. It was explained in detail. So, the way how you get cookie is you create middleware file in the root folder as you are using nextJs > 13 version
You can check if cookie exist inside middleware by:
|
Beta Was this translation helpful? Give feedback.
-
Summary
Hello, I am new to the world of App Router.
I have a Fetch API wrapper to create a new service with code like this:
This is work well on App Router(RSC). But the problem is, coz my components don't all use server components (some use client components), I get an error regarding cookie headers(
import { cookies } from 'next/headers'
) that can't be used in client components when i callcreateService
wrapper in client components.So is there a solution to this problem? Where can this
createService
function be used universally in RSC as well as client components? Thank You.Additional information
Example
No response
Beta Was this translation helpful? Give feedback.
All reactions