React Server Components (RSC) render on the server, leaving no JS for the browser to run.
My question is, can we access and manipulate the DOM in a RSC?
For instance, let's say we want to manipulate CSS variables based on data fetched in a RSC. We can accomplish that with document.documentElement.style.setProperty("--foo", foo);
. Since document
is not available server-side, we have to make the component a React Client Component (RCC). foo
would be fetched in a RSC and passed as a prop into the RCC. However, it would be ideal if the server could handle the DOM manipulation as well, so that the HTML+CSS+JSON bundle served to the client (browser) by the RSC already contained the changes we know we want to make at build-time.
Is it possible to do the DOM manipulation server-side, in the RSC? If so, how?