Next.js Discord

Discord Forum

getServerSession not working

Answered
Pollock posted this in #help-forum
Open in Discord
PollockOP
getServerSession isn't getting the nextauth session when I call a fetch from a server component
Answered by fuma
You shouldn’t fetch route handlers from server components, just invoke the function, or fetch it on the client side.
View full answer

22 Replies

You shouldn’t fetch route handlers from server components, just invoke the function, or fetch it on the client side.
Answer
Since you’re trying to fetch it from server, cookies won’t be sent to your route handler.
@fuma You shouldn’t fetch route handlers from server components, just invoke the function, or fetch it on the client side.
PollockOP
I was having an issue where the data is cached when I hit back on the browser button. I figured maybe I could use fetch's no cache setting to get this to work
Unfortunately, when you are navigating back, Next.js won’t re-fetch the page because of router cache, your page will be cached at client side.
A workaround is to use router.refresh, or fetch it on the client side via useEffect/libraries like SWR.
PollockOP
Hmm maybe I should use the client side approach. I was just wanting to follow the serverside paradigm of nextjs
How would i use router.refresh though? I tried it before and it was an infinte loop
How do you use it?
You can call it in a useEffect, or something that only executes on client
PollockOP
Perhaps I'm thinking of when I tried redirect()
thanks for the help 🙂
@fuma You shouldn’t fetch route handlers from server components, just invoke the function, or fetch it on the client side.
Carolina Dog
Why do they make it like this? I want it to make the GET logic stays in the route handler. If I fetch the data on the client side, client components can't event use async, so That's a problem?
He said server components not client components
@aardani He said server components not client components
Carolina Dog
" just invoke the function, or fetch it on the client side."
Why do you need to call an endpoint of your server from your server?
It doesn't make sense, the worst implementation ever
thats why it doesn't make sense to GET fetch in the server to your own server :/
invoking the function means you create a function that you can use both in Route Handler and Server Components
Thank you guys