cookies
Answered
Palomino posted this in #help-forum
PalominoOP
I wrote a function to use my API, and I feed cookies into it. I have used the SSR function cookies() from next/headers. but when im doing it clientside I cant figure how to use cookies. is there a way to get ReadonlyRequestCookies in client?
Answered by B33fb0n3
first things first:
1. Don't fetch your own components [(read more)](https://nextjs-faq.com/fetch-api-in-rsc)
2. Why don't you use the
when using the cookies() function on the server, it will also get the cookies
You need to solve "1." first to make "2." work
1. Don't fetch your own components [(read more)](https://nextjs-faq.com/fetch-api-in-rsc)
2. Why don't you use the
cookies() function inside your api route handler? It will be serverside and cookies() will be available there as well, when you call it from the client.when using the cookies() function on the server, it will also get the cookies
You need to solve "1." first to make "2." work
19 Replies
@Palomino I wrote a function to use my API, and I feed cookies into it. I have used the SSR function cookies() from next/headers. but when im doing it clientside I cant figure how to use cookies. is there a way to get ReadonlyRequestCookies in client?
why don't you use the cookies() function inside your api route?
@B33fb0n3 why don't you use the cookies() function inside your api route?
PalominoOP
The function I wrote, is ran on the server. so cookies would be null
example how I use the function in ssr component
@Palomino example how I use the function in ssr component
first things first:
1. Don't fetch your own components [(read more)](https://nextjs-faq.com/fetch-api-in-rsc)
2. Why don't you use the
when using the cookies() function on the server, it will also get the cookies
You need to solve "1." first to make "2." work
1. Don't fetch your own components [(read more)](https://nextjs-faq.com/fetch-api-in-rsc)
2. Why don't you use the
cookies() function inside your api route handler? It will be serverside and cookies() will be available there as well, when you call it from the client.when using the cookies() function on the server, it will also get the cookies
You need to solve "1." first to make "2." work
Answer
PalominoOP
ah right
i forgot it only works in dev
@Palomino i forgot it only works in dev
yea, happens the best 🙂
PalominoOP
I got alot of code to rewrite now 😂
but, my useApi function is made like this
https://pastecode.dev/s/yx4o9gwd
https://pastecode.dev/s/yx4o9gwd
cuz its litterally my server making the fetch, not a server component
PalominoOP
WOAH
you were right
server still got my cookies
oh
functions I write is ran in the client
@Palomino server still got my cookies
yea, it#s pretty cool how everything works together when you follow best practices 😉
Is your initial issue solved like that?
Is your initial issue solved like that?
@Palomino solved?
that's not the solution. I marked the correct one. You don't need to do anything else