Next.js Discord

Discord Forum

Access URL from Client Component or Server Component?

Answered
Da_v_id posted this in #help-forum
Open in Discord
Avatar
Hi! Is it better to access the slug in the URL directly from the client component or server component and pass it down as a prop to the client? Is there any differences in performance?
Answered by gin
server component for sure
View full answer

8 Replies

Avatar
server component for sure
Answer
Avatar
cause rsc loads before the client
and if u pass it down the client will directly have the value without doing any kind of javascript to read the url
Avatar
oh i see interesting! And quick question, is it possible to pull the params from the nested server component, or do i have to prop drill from the page.tsx's params?
^
Avatar
There is ways to do that but i wouldnt really recommend, usually u pull the params in your layout or page and do the big operations near those files, then u pass the information down to your client or whatever. You could read the params from the middleware and set it as a custom header to read from any rsc
Or you could read the params into some kind of cache in page or layout and access it from any other rsc aswell, tho this one is also not recommended cause of the way nextjs works (the flow is not persistent)
Avatar
I will keep these in mind for the future, for now I will just prop drill from the layout! Thank you so much!