Next.js Discord

Discord Forum

Next.js neither sends nor sets cookies in RSC

Unanswered
Youssef posted this in #help-forum
Open in Discord
I'm fetching my API domain on a server component with the fetch API
The problem is every time I fetch it on the server, I have to set all cookies explicitly within the headers which is so annoying and reduplicated, otherwise they won't be sent

Example:
export default async function Home() {
    const res = await fetch("my-api", {
        credentials: "include",
        headers: {
            Cookie: `somecookie=${cookies().get("somecookie")?.value}`, // If I removed this line, the cookie wouldn't be sent
        },
    });
}

though I configured Access-Control-Allow-Origins and Access-Control-Allow-Credentials headers on my API server

20 Replies

that fetch req is run on your server not client
so, i don't see the issue - you should do the fetch on client or?
and is the "my-api" from the same next server? if so: https://nextjs-faq.com/fetch-api-in-rsc
@riský so, i don't see the issue - you should do the fetch on client or?
I can do it, tho I attemp to get benefit of RSC
but your solution seems to be the best for passing the cookie (as you want server comp)
@riský but your solution seems to be the best for passing the cookie (as you want server comp)
It works but sending all cookies for each request is kinda tough
so I'm looking for an easier way if any
yeah but why would nextjs send the same headers to the servers req?
you could make a simple wrapper for fetch that adds the user cookie
@riský yeah but why would nextjs send the same headers to the servers req?
It'd be better if this was configurable
i think it would cause more people trying to do it... as it is kinda an antipattern imo
so i think custom wrapper can add the cookie everywhere and have your desired outcome
Ok I'll try
It works in RSC, but in middleware I get this:
o.O
ig you should be getting the cookies from the req payload instead of that
yup I forgot xd
thanks