Use client
Answered
Shikoku posted this in #help-forum
ShikokuOP
Is it possible to convert page.tsx file into client component by mentioning 'use client on the top of it. What are the pros and cons of doing this.
Answered by Polar bear
Yes you can, but the whole page will be considered as a client component and its children components too (even though there is a trick to render a server component going through a client component). This is not recommended since you will lose all the benefits of server components such as fetching data on the server, SEO benefits, less JS code to send to the user and more.
https://www.youtube.com/watch?v=Qdkg_mrniLk&t=3s&ab_channel=ByteGrad
https://www.youtube.com/watch?v=Qdkg_mrniLk&t=3s&ab_channel=ByteGrad
29 Replies
Polar bear
Yes you can, but the whole page will be considered as a client component and its children components too (even though there is a trick to render a server component going through a client component). This is not recommended since you will lose all the benefits of server components such as fetching data on the server, SEO benefits, less JS code to send to the user and more.
https://www.youtube.com/watch?v=Qdkg_mrniLk&t=3s&ab_channel=ByteGrad
https://www.youtube.com/watch?v=Qdkg_mrniLk&t=3s&ab_channel=ByteGrad
Answer
ShikokuOP
Alright thank you
Masai Lion
There is no SEO benefit with client components?
Siamese Crocodile
Yes, client components do not directly improve SEO.
Masai Lion
they are supposedly pre rendered on client
should not they be accessible for google bot?
should not they be accessible for google bot?
Siamese Crocodile
Client-side components, although rendered on the client, might not always be fully accessible or indexed by Googlebot in the same way that server-rendered content or static content would be.
Masai Lion
how to make them fully accessible?
@Masai Lion they are supposedly pre rendered on client
should not they be accessible for google bot?
Polar bear
Yes, they are pre-rendered, but you will only ship the initial HTML you can build on the server.
For example, if your UI depends on fetch calls that are made on the client side (using useEffect for example) you will need to wait for the user to download the JS code from the page to be able to fetch the data, and then you will render the full page.
However, if you do these db/fetch calls on the server, you can ship the whole HTML code directly to the user without waiting for the JS code to be downloaded and this is good for SEO.
You can look at the benefits of server side rendering in the Next.js doc:
https://nextjs.org/docs/app/building-your-application/rendering/server-components#benefits-of-server-rendering
For example, if your UI depends on fetch calls that are made on the client side (using useEffect for example) you will need to wait for the user to download the JS code from the page to be able to fetch the data, and then you will render the full page.
However, if you do these db/fetch calls on the server, you can ship the whole HTML code directly to the user without waiting for the JS code to be downloaded and this is good for SEO.
You can look at the benefits of server side rendering in the Next.js doc:
https://nextjs.org/docs/app/building-your-application/rendering/server-components#benefits-of-server-rendering
In addition to that, client components is not a deoptimization. It can affect SEO but it can also not affect SEO if the initial state is provided from the server
@Alfonsus Ardani In addition to that, client components is not a deoptimization. It can affect SEO but it can also not affect SEO if the initial state is provided from the server
Masai Lion
that is where my problem lies
say my backend is a separate service available at api.com
say i have a route /product/123 which has link to another product - /product/124
if my product component is composed of a server comp (for data fetching from api.com) and client component for showing UI
if user navigates to /product/124, it implies one hit on the nextjs server just for fetching data from api.com and then rendering it back on browser.
while this will be good for SEO, i am making one unnecessary call to server..
say my backend is a separate service available at api.com
say i have a route /product/123 which has link to another product - /product/124
if my product component is composed of a server comp (for data fetching from api.com) and client component for showing UI
if user navigates to /product/124, it implies one hit on the nextjs server just for fetching data from api.com and then rendering it back on browser.
while this will be good for SEO, i am making one unnecessary call to server..
yes but
you can cache your respond of /prroduct/124
and let the static part to be for SEO and have the dynamic part to be whatever your user needs that doesnt need SEO
Masai Lion
yes got that but if the api called for /product/124 is personalized per user i dont have any caching benefit too
in that case, why would you need SEO if /product/124 is personalized per user...
just serve static content (that doesn't require hitting /product/124) and let client do the fetching to /product/124
Masai Lion
for a non-registered user i still want to index the metadata of product 124 on google
I cant go with static generation because the data for 124 keeps changing periodically..
I cant go with static generation because the data for 124 keeps changing periodically..
You can use server component to generate the metadata of product 124, and then use client component to update its data as requested. The one on the server component can be cached as to not get refreshed every user request but for everry 3-5 seconds, or 1 hour? idk. Something like that
Masai Lion
yes agree, but say user navigates on the browser from product 124 to 125 -->
1. now again a hit on nextjs server + make api call or mostly used cached value from next cache
2. make api call from client to get actual personalized data
i was looking for a way to avoid step #1
1. now again a hit on nextjs server + make api call or mostly used cached value from next cache
2. make api call from client to get actual personalized data
i was looking for a way to avoid step #1
sooo basically shallow routing
not familiar yet with that maybe you can create a new post.
what im trying to say is that if you did a fetch in the server, converting a server comp to client comp wont affect the SEO if set properly
i would still not recommend converting the entire page with "use client"
@Alfonsus Ardani what im trying to say is that if you did a fetch in the server, converting a server comp to client comp wont affect the SEO if set properly
Masai Lion
just one more clarification, if i convert server comp to client comp - how do i fetch data in the client component during the server rendering phase?
i have been understanding its only possible with Server Component passing data to Client component w next15 app router
i have been understanding its only possible with Server Component passing data to Client component w next15 app router
@Alfonsus Ardani not familiar yet with that maybe you can create a new post.
Masai Lion
i did have a post but did not get traction yet : https://nextjs-forum.com/post/1314688327764414494
need to learn more of shallow routing, based on another response i understood it needs more testing and not super elegant way: https://nextjs-forum.com/post/1194236734821642291#message-1314821724285567059
need to learn more of shallow routing, based on another response i understood it needs more testing and not super elegant way: https://nextjs-forum.com/post/1194236734821642291#message-1314821724285567059
@Masai Lion just one more clarification, if i convert server comp to client comp - how do i fetch data in the client component during the server rendering phase?
i have been understanding its only possible with Server Component passing data to Client component w next15 app router
its hard to fetch data in client component during server rendering phase. Because effectively, you are like "emulating" the client component in the server. Even if you managed to fetch those data in the client comp in the server, its hard to prevent the server logic into running in the client due to how the hydration process is done.
Also that you'd be importing uneccesary server code into the client which can increase bundle size
Also that you'd be importing uneccesary server code into the client which can increase bundle size
Its better to fetch in server component and pass it to a client component
the idea that client component are prerendered is to promote not only unaffected SEO, but also progressive enhancement. Which is speaking that if you have JS disabled, it still have the initial styles of the client component, and the data that was passed from the server. (as a side effect, doesn't kill off SEO if you fetched it from the server)
You can bump the thread once a day just fyi