Sobes.tech
Middle

Are you familiar with Next.js? What is the difference between SSR, SSG, ISR, and CSR? When would you use each?

sobes.tech AI

Answer from AI

Next.js offers several methods for rendering pages:

  • SSR (Server-Side Rendering) — the page is generated on the server for each request. Suitable for dynamic content that changes frequently and needs to be up-to-date.

  • SSG (Static Site Generation) — pages are generated once during build time and served as static files. Good for content that rarely changes, such as a blog or documentation.

  • ISR (Incremental Static Regeneration) — a hybrid of SSG and SSR. Allows updating static pages after deployment with a specified interval (revalidate). Used when you need to serve static content quickly but also want to update it without a full rebuild.

  • CSR (Client-Side Rendering) — rendering occurs on the client in the browser using JavaScript. Used for interactive parts of the application where data is loaded dynamically after the page loads.

When to use:

  • SSR — for pages with frequently changing data requiring SEO.
  • SSG — for static content with infrequent updates.
  • ISR — when you want to combine the benefits of SSG and the ability to update data without a full rebuild.
  • CSR — for user dashboards where SEO is not critical and interactivity is important.

Example:

// SSR
export async function getServerSideProps() {
  const data = await fetchData();
  return { props: { data } };
}

// SSG
export async function getStaticProps() {
  const data = await fetchData();
  return { props: { data } };
}

// ISR
export async function getStaticProps() {
  const data = await fetchData();
  return { props: { data }, revalidate: 60 };
}
Are you familiar with Next.js? What is the difference… - sobes.tech