how to handle data fetching on server vs client side with cookies
Unanswered
Common Moorhen posted this in #help-forum
Common MoorhenOP
when building services to fetch data on my next JS project, I often find myself building conditionals for adding cookies on the headers or not.
because the request can be performed from a client or server component, and depending on whether I can need the cookie header or don't.
I was thinking of building my own function that checks for
is there any standard solution for this? I guess is a problem devs would face frequently enough, so I imagine there would be a solution already built, or a standard way to do it.
because the request can be performed from a client or server component, and depending on whether I can need the cookie header or don't.
I was thinking of building my own function that checks for
window type and based on that gets the cookie and adds it to the headers, but I don't want to reinvent the wheel.is there any standard solution for this? I guess is a problem devs would face frequently enough, so I imagine there would be a solution already built, or a standard way to do it.
3 Replies
@Common Moorhen when building services to fetch data on my next JS project, I often find myself building conditionals for adding cookies on the headers or not.
because the request can be performed from a client or server component, and depending on whether I can need the cookie header or don't.
I was thinking of building my own function that checks for `window` type and based on that gets the cookie and adds it to the headers, but I don't want to reinvent the wheel.
is there any standard solution for this? I guess is a problem devs would face frequently enough, so I imagine there would be a solution already built, or a standard way to do it.
try this
// fetching on server
import { headers } from 'next/header'
fetch("url", { headers: new Headers(headers()) })
// fetching on client
fetch("url", { credentials: "include" })Common MoorhenOP
oh, so i guess it doesnt apply to me
im using a reverse proxy