Next.js Discord

Discord Forum

Will `getData` in a RSC page ever be called on the client?

Unanswered
Black and yellow mud dauber posted this in #help-forum
Open in Discord
Black and yellow mud dauberOP
Hello!
I am not sure my understanding of fetching in server component is corret.
I have a page without "use client", it has a getData function that does a fetch.

On first page load, it happens on the server, I'm happy.
Is it expected it can run on the client side as well? I would have thought it should not.
I'm accessing process.env in there, and the URL I'm fetching from would cause CORS if the browser tries.

(Disclaimer, in my app, it does execute that code in the browser later on, and of course things go wrong).

19 Replies

It won't be associated with any client-side JS
perfectly safe
check the "data layer" bit
and fetching from server instead of browser to avoid having to setup CORS is exactly the kind of benefit we look for when preferring server-side data fetching
you can keep your internal API, which is probably the part that should be the most secure, totally private, only your own Next app can query it via the server
"(Disclaimer, in my app, it does execute that code in the browser later on, and of course things go wrong).
" please elaborate if you have an unexpected bug though
Black and yellow mud dauberOP
Then I suppose I have some unnexpected bugs.
I'm moving an existing mostly client side app to RSC.
I removed use client; and moved the fetching from the useEffect into getData
It works, but when I interact in the page, there is a rerender in my getData is called.
In my app/pages.tsx I've added a check to make it more obvious.
How would I debug that and find what causes this?
you don't show enough code sorry
it depends on how the component is structured, and how you use it
you might import it into another client component
try importing "server-only"
that's an helper from React
if you import it, it will throw => you will be able which component is responsible of incorrectly importing this code
and it will be safer
Black and yellow mud dauberOP
Thank you! I'll try that!
Black and yellow mud dauberOP
That is exactly what you said! I was importing it in another component that still had "use client" .
Thank you very much!