I am baffled by the lack of concrete information about how Client-Side Rendering (CSR), Server-Side Rendering (SSR), and Static Site Generation (SSG) actually work.
There are tons of articles loosely explaining the concepts, but I have not found a single source that goes in-depth and explains exactly what HTML, CSS and JS are created on the server vs on the client, in the three concepts.
From the Next.js docs, we can read that:
- With Static Rendering, both Server and Client Components can be prerendered on the server at build time.
- Client Components have their HTML and JSON prerendered and cached on the server. The cached result is then sent to the client for hydration.
- Server Components are rendered on the server by React, and their payload is used to generate HTML. The same rendered payload is also used to hydrate the components on the client, resulting in no JavaScript needed on the client.
Note: Static Rendering is equivalent to Static Site Generation (SSG) and Incremental Static Regeneration (ISR).
And:
- With Dynamic Rendering, both Server and Client Components are rendered on the server at request time.
Note: This is equivalent to Server-Side Rendering(getServerSideProps()).
Fine, so Static Rendering equals Static Site Generation (SSG), meaning that some html+css is created build-time. Exactly what html is created build-time depends on whether you use Client Components or Server Components.
I inspected the built public
folder of a built Gatsby project and noticed that none of the React components were actually turned into html (regardless of whether they depended on user interaction). Gatsby had created an html file for each page, but the <body>
of each of those was still React components defined in JS, expecting to be turned into html by React on the client, run-time. This breaks with my understanding of the second bullet from the Next.js docs, which states that the html is "prerendered".
Questions
By the above docs, could we say that Static Site Generation (SSG) actually requires both Server-Side Rendering (SSR) and Client-Side Rendering (CSR)?
If so, does that not conflict with the Dynamic Rendering definition above, which states that Dynamic Rendering is equivalent to Server-Side Rendering?
Finally, exactly what html+css+js is created upon Static Rendering with Client Components?