React Interview Questions (Part 9): Server-Side Rendering (SSR) and Static Site Generation (SSG)

Yusuf UysalYusuf Uysal
3 min read

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).
0
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.