React Interview Questions (Part 9): Server-Side Rendering (SSR) and Static Site Generation (SSG)
Table of contents
- 1. What is server-side rendering (SSR), and how does it differ from client-side rendering (CSR)?
- 2. How does Next.js handle SSR in React applications?
- 3. What is static site generation (SSG), and how does it improve performance?
- 4. What are the benefits and drawbacks of using SSR in React applications?
- 5. When would you choose SSR or SSG over client-side rendering?
1. What is server-side rendering (SSR), and how does it differ from client-side rendering (CSR)?
Server-side rendering (SSR) generates HTML on the server for each request, improving initial load times and SEO. In contrast, client-side rendering (CSR) builds the HTML in the browser using JavaScript, which can delay the display of content.
In SSR, the HTML content is ready before it reaches the browser, which reduces the time-to-first-paint. With CSR, the browser first downloads and executes JavaScript before rendering the content, making the initial load slower but enabling faster client-side interactions after that.
2. How does Next.js handle SSR in React applications?
Next.js natively supports server-side rendering (SSR). It uses the getServerSideProps
function to fetch data and render it on the server before sending the HTML to the client.
export async function getServerSideProps() {
const data = await fetch('https://api.example.com/data');
return { props: { data } };
}
function Page({ data }) {
return <div>{data}</div>;
}
The getServerSideProps
function runs at request time, fetching fresh data and rendering it server-side. This means that each time the page is requested, the server will process the data and return the up-to-date HTML.
By default, Next.js performs SSR for all pages unless explicitly configured otherwise (e.g., for static pages or client-side rendering).
3. What is static site generation (SSG), and how does it improve performance?
Static Site Generation (SSG) pre-builds HTML pages at build time, meaning the content is generated once and served statically. SSG is ideal for content that doesnโt change frequently, such as documentation, blogs, or marketing pages.
export async function getStaticProps() {
const data = await fetch('https://api.example.com/static-data');
return { props: { data } };
}
function Page({ data }) {
return <div>{data}</div>;
}
SSG significantly improves performance by reducing server load. It can take advantage of Content Delivery Networks (CDNs) to cache the static HTML and serve it quickly to users across the globe. This results in faster load times, especially when serving the same content repeatedly.
4. What are the benefits and drawbacks of using SSR in React applications?
Benefits of SSR:
Improved SEO: Pre-rendered HTML is crawled by search engines, improving SEO.
Faster Initial Load: Content is available immediately on load.
Drawbacks of SSR:
Server Load: More processing on the server can slow it down with high traffic.
Less Interactivity: SSR isnโt ideal for applications needing heavy client-side interactions.
5. When would you choose SSR or SSG over client-side rendering?
Use SSR:
For dynamic pages where content frequently changes (e.g., e-commerce product pages, user-generated content).
For SEO-focused pages where visibility in search engines is important (e.g., blogs, landing pages).
Use SSG:
For static content that doesnโt change often (e.g., blogs, documentation).
For performance: SSG provides faster load times due to cached static content, especially with CDNs.
Use CSR:
- For highly interactive web apps (e.g., dashboards, where most updates happen on the client-side after the initial page load).
Subscribe to my newsletter
Read articles from Yusuf Uysal directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Yusuf Uysal
Yusuf Uysal
๐๐ฟ๐ถ๐๐ฒ๐ป ๐๐ฟ๐ผ๐ป๐๐ฒ๐ป๐ฑ ๐๐ฒ๐๐ฒ๐น๐ผ๐ฝ๐ฒ๐ฟ with extensive experience in JavaScript, React.js, and Next.js. I help companies enhance user engagement and deliver high-performance web applications that are responsive, accessible, and visually appealing. Beyond my technical skills, I am a ๐ฐ๐ผ๐น๐น๐ฎ๐ฏ๐ผ๐ฟ๐ฎ๐๐ถ๐๐ฒ ๐๐ฒ๐ฎ๐บ ๐ฝ๐น๐ฎ๐๐ฒ๐ฟ who thrives in environments that value continuous learning and innovation. I enjoy projects where I can leverage my expertise in ๐ฟ๐ฒ๐๐ฝ๐ผ๐ป๐๐ถ๐๐ฒ ๐ฑ๐ฒ๐๐ถ๐ด๐ป, ๐ฏ๐ฟ๐ผ๐๐๐ฒ๐ฟ ๐ฐ๐ผ๐บ๐ฝ๐ฎ๐๐ถ๐ฏ๐ถ๐น๐ถ๐๐, ๐ฎ๐ป๐ฑ ๐๐ฒ๐ฏ ๐ฝ๐ฒ๐ฟ๐ณ๐ผ๐ฟ๐บ๐ฎ๐ป๐ฐ๐ฒ to deliver seamless user experiences. I get excited about opportunities where I can contribute to creating dynamic, user-focused applications while working closely with cross-functional teams to drive impactful results. I love connecting with new people, and you can reach me at yusufuysal.dev@gmail.com.