Nextjs Dynamic server usage
By using headers in a function to get the userid on the server side, my page isn’t rendered statically anymore.
Is there an actual clean server side logic to get the user Id?
12 Replies

Getting a user ID in a server side render will never be static, by definition
Yes that’s what I also though but in clerk it was
It's just not possible
Or am I wrong
If you have dynamic data its not static
How can I though then suppress all my build warnings
When I build my page
Clerk likely used some sort of React context provider which is client side session
if it was "static"
TBH it doesn't make a ton of sense to get the user ID in an SSR render. You need to use the useSession hook on the client anyways so that it stays up to date
You only want user ID server side if you are doing mutations or api requests
Yes and that’s what I am doing
If you are using useSession hook client side and also fetching session on the render it leads to a double fetch
Your server will query the data on the render and then your client will send a request to your API which will query the db again
or use cookie cache, but still its duplicated
Your page should just use
Then you can have a static page, very fast and cheaper if hosting on a service like Vercel
Anywhere you are using your userId on a React component you want to have "use client" directive
Okay thanks, yes I am self-hosting though.
So generally speaking its always smart to static render all content which is not connected to an user. (On protcted pages).
And then on client side, we fetch via api / trpc calls the user information?
Yea dynamic information is good to be fetched from client. But for sessions you definitely want to use the useSession hook or my better-auth-tanstack package
Which has TS query hooks for Better Auth