Konabos

Understanding Rendering in Next.js with Sitecore XM Cloud: SSG, SSR, ISR, and CSR

Akshay Sura - Partner

24 Feb 2025

Share on social media

Next.js is a versatile framework that pairs exceptionally well with Sitecore XM Cloud, enabling developers to deliver seamless, headless digital experiences. Sitecore XM Cloud's composable architecture and Next.js's flexible rendering strategies provide a robust solution for modern web development. Let's explore Static Site Generation (SSG), Server-Side Rendering (SSR), Incremental Static Regeneration (ISR), and Client-Side Rendering (CSR) within the context of Sitecore XM Cloud, discussing their advantages, disadvantages, and how to use them effectively in a headless environment.

Static Site Generation (SSG)

SSG pre-renders HTML at build time, creating static files that can be served from a CDN. When paired with Sitecore XM Cloud, it ensures lightning-fast delivery of content that doesn't change frequently.

Advantages:

  1. Fast Load Times: Static files hosted on a CDN provide near-instant loading.
  2. SEO-Friendly: Search engines can easily crawl the pre-rendered HTML.
  3. Reduced API Requests: Pages are built once, minimizing calls to Sitecore's content delivery APIs.

Disadvantages:

  1. Content Staleness: Updates to Sitecore content require a rebuild and redeployment.
  2. Long Build Times: Large sites with numerous pages can experience slow builds.

Ideal Use Cases:

  • Marketing sites, landing pages, or blogs with infrequent content updates.
  • Static pages that need to be delivered at scale.

Example Implementation:

export async function getStaticProps() {

  const data = await fetch('https://your-sitecore-instance/api/content');

  return {

    props: { data },

  };

}

Server-Side Rendering (SSR)

SSR renders pages on the server for each request, fetching live data from Sitecore XM Cloud's APIs and delivering fully rendered HTML to users.

Advantages:

  1. Dynamic Content: Ensures users receive the latest Sitecore content in real-time.
  2. SEO Benefits: Fully rendered HTML is ready for search engines to index.
  3. Personalization Ready: Integrates seamlessly with Sitecore's personalization features.

Disadvantages:

  1. Higher Latency: Rendering pages on demand can increase response times.
  2. Increased Server Load: Each request triggers API calls and rendering logic.

Ideal Use Cases:

  • Personalization-heavy pages.
  • Real-time dashboards or user-specific content.

Example Implementation:

export async function getServerSideProps(context) {

  const data = await fetch('https://your-sitecore-instance/api/content');

  return {

    props: { data },

  };

}

Incremental Static Regeneration (ISR)

ISR bridges the gap between SSG and SSR, allowing static pages to be updated incrementally at runtime. This makes it a powerful choice for Sitecore XM Cloud integrations. This is my favorite method for Next.js. Depending on your client's needs and where the sites are accessed, you may have to use reverse proxies if your website requests unusual ports or URLs that can be aborted on corporate firewalls. We had to end up using HA Poxy to solve the issue. This also allowed us to embed the authentication keys in HA Proxy instead of doing it on the client calls.

Advantages:

  1. Fresh Content: Updates pages without requiring a full rebuild.
  2. Performance: Serves static content while enabling periodic updates.
  3. Scalable: Reduces API load by regenerating pages only when necessary.

Disadvantages:

  1. Complexity: Requires managing revalidation intervals effectively.
  2. Slight Delays: Content updates may not appear immediately.

Revalidation Strategies in ISR:

  • Time-Based Revalidation:
    • Pages are revalidated at specified intervals using the revalidate property in getStaticProps.
    • Example:

export async function getStaticProps() {

  const data = await fetch('https://your-sitecore-instance/api/content');

  return {

    props: { data },

    revalidate: 60, // Revalidate every 60 seconds

  };

}

  • On-Demand Revalidation:
    • Allows triggering revalidation manually via an API route.
    • Useful for scenarios like publishing new content in Sitecore.
    • Example:

export default async function handler(req, res) {

  await res.revalidate('/path-to-revalidate');

  return res.json({ revalidated: true });

}

  • Background Revalidation:
    • Ensures the page is served immediately to users while revalidation happens in the background.
    • Minimizes delays in content delivery for users.

Ideal Use Cases:

  • E-commerce product pages with inventory updates.
  • News articles or blogs with frequent updates.

Client-Side Rendering (CSR)

CSR fetches and renders content in the browser after the initial page load. While it's less common for Sitecore XM Cloud projects, it can be useful for highly interactive components.

Advantages:

  1. Dynamic Interactions: Ideal for components that require frequent updates without reloading the page.
  2. Reduced Server Load: Rendering is offloaded to the client.
  3. Simplified API Calls: Fetches Sitecore content directly from the browser.

Disadvantages:

  1. SEO Challenges: Search engines may struggle with indexing JavaScript-rendered content.
  2. Slower Initial Load: Content is fetched post-load, which can impact perceived performance.

Ideal Use Cases:

  • Single-page applications (SPAs).
  • Pages with complex client-side interactions.

Example Implementation:

import { useEffect, useState } from 'react';

function Page() {

  const [data, setData] = useState(null);

  useEffect(() => {

    fetch('https://your-sitecore-instance/api/content')

      .then((response) => response.json())

      .then(setData);

  }, []);

  return <div>{data ? JSON.stringify(data) : 'Loading...'}</div>;

}

Leveraging Vercel's Data Cache for Optimized Fetch Requests

When integrating Next.js and Sitecore XM Cloud, Vercel's Data Cache efficiently reduces API calls and improves application performance. By caching responses from Sitecore's APIs, you can:

  1. Minimize Redundant Requests: Store and reuse API responses to reduce the load on Sitecore's content delivery APIs.
  2. Enhance Performance: Serve cached data directly to users, improving page load times.
  3. Control Cache Lifetime: Use cache-control headers to define caching rules and expiration policies.

Example Implementation with Data Cache:

export async function getServerSideProps() {

  const res = await fetch('https://your-sitecore-instance/api/content', {

    next: { revalidate: 60 }, // Cache response for 60 seconds

  });

  const data = await res.json();

  return {

    props: { data },

  };

}

By leveraging Vercel's caching mechanism, you can balance performance with content freshness, ensuring your application scales effectively while delivering an optimal user experience.

Combining Next.js with Sitecore XM Cloud opens up powerful possibilities for delivering headless digital experiences. Choosing the right rendering strategy—SSG, SSR, ISR, or CSR—depends on your application's specific performance, personalization, and content freshness requirements. By leveraging Sitecore XM Cloud's robust APIs and Next.js's flexibility, alongside tools like Vercel's Data Cache, developers can build scalable, efficient, and engaging applications tailored to their audience.

Do you have questions about integrating Sitecore XM Cloud with Next.js? Contact us today to explore how we can help you optimize your headless architecture!

Sign up to our newsletter

Share on social media

Akshay Sura

Akshay Sura

Akshay is a nine-time Sitecore MVP and a two-time Kontent.ai. In addition to his work as a solution architect, Akshay is also one of the founders of SUGCON North America 2015, SUGCON India 2018 & 2019, Unofficial Sitecore Training, and Sitecore Slack.

Akshay founded and continues to run the Sitecore Hackathon. As one of the founding partners of Konabos Consulting, Akshay will continue to work with clients to lead projects and mentor their existing teams.


Subscribe to newsletter