Access URL from Client Component or Server Component?
Answered
Da_v_id posted this in #help-forum
Da_v_idOP
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?
8 Replies
server component for sure
Answer
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
Da_v_idOP
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?
^
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)
Da_v_idOP
I will keep these in mind for the future, for now I will just prop drill from the layout! Thank you so much!