Will getServerSession from next-auth be deduped if called multiple times on the same page
Answered
Plott Hound posted this in #help-forum
Plott HoundOP
Say i have a page with some nested components that both use getServerSession from nextauth. will these call be deduped? i'm trying to avoid calling it further up the tree and prop drilling it down many levels. each time im just retrieving the user ID to make db queries.
Edit: I'm also using prisma adapter and db sessions if that changes things
Edit: I'm also using prisma adapter and db sessions if that changes things
Answered by Jesse
around your
You can have a console.time and console.timeEnd. And you can test how long it takes the first time, and the second time, to see if it's being de duped
await getServerSideSession().You can have a console.time and console.timeEnd. And you can test how long it takes the first time, and the second time, to see if it's being de duped
28 Replies
For this, I would just use a state management solution like Zustand to avoid prop drilling but still have the user accessible everywhere. However if that's not a viable solution, I would guess that it's not being called again since Next is aggressive on caching and this sort of thing.
For example when you make a fetch requests in your layout.tsx and then in your page.tsx it's not called multiple times
But I still think using Zustand or a state management solution is the best way for what you want to do.
Plott HoundOP
thanks but i need the userID in RSC which i dont think is possible with state managemnent.
its for making server side db queries
oh yeah sorry, mb
in that case
Plott HoundOP
no problem thanks for trying!
but that would be great if i just get the id when they login and i can access it without async
around your
You can have a console.time and console.timeEnd. And you can test how long it takes the first time, and the second time, to see if it's being de duped
await getServerSideSession().You can have a console.time and console.timeEnd. And you can test how long it takes the first time, and the second time, to see if it's being de duped
Answer
if it is then the second time should be significantly faster than the first
I know it's a bit weird but it should answer your question
@Jesse around your `await getServerSideSession()`.
You can have a console.time and console.timeEnd. And you can test how long it takes the first time, and the second time, to see if it's being de duped
Plott HoundOP
thanks i'll try that when i get home!
@Plott Hound thanks i'll try that when i get home!
no problem, tell me if it works
Plott HoundOP
will do
usually i just call it once in the main page.tsx and prop drill it down wherever its needed but in this situation it would require so much drilling that i'd rather just call it twice if it is indeed deduped
worst case scenario if it was being de duped and you wanted the best performance you could put it in a cookie and then get it in the RSC, but that's a bit extreme if you ask me
@Jesse did it work
Plott HoundOP
sorry Jesse i got distracted. i promise i will test it in the next 30mins and get back to yuo
@Plott Hound sorry Jesse i got distracted. i promise i will test it in the next 30mins and get back to yuo
nah it's fine was just wondering if it worked
@Jesse nah it's fine was just wondering if it worked
Plott HoundOP
nah my bad i always get mad when people forget to respond in their help topics. i'll get it done today!
That function also accepts a parameter
Which is your next auth config
Passing it will circumvent a network call
Which will speed up get server session responses
And no they will not be deduped
But i wouldn’t worry about it unless you’re seeing performance issues