blog   |   10 Jan, 2022

CSR vs SSR vs SSG? Choosing the right rendering mode for your SPA

In modern web development, the choice of rendering mode for Single Page Applications (SPAs) holds paramount importance. The methods of rendering i.e Client-Side Rendering (CSR), Server-Side Rendering (SSR), and Static Site Generation (SSG) carry distinct characteristics, each catering to different requirements and scenarios. Understanding their nuances can significantly impact the performance, user experience, and the search engine optimization of your application.

 

Understanding SPA rendering modes

1. Client-side rendering (CSR)

Client-side rendering, as the name suggests, renders the site on the client side i.e the browser. Upon an initial URL request, the server delivers a minimal HTML file, often just a shell of the webpage, alongside some assets and the Javascript to manipulate the DOM and render the contents of the page.

However, the primary challenge with CSR lies in search engine optimization (SEO). Search engines depend on crawling and parsing HTML content to index the web pages. With the initial empty or near-empty DOM loaded in CSR, search engine bots might struggle to interpret and index the complete content, compromising the site's SEO. That's where our second rendering mode comes in, i.e the SSR mode.

2. Server-side rendering (SSR)

In SSR, a server, often a Node server, manages the initial rendering of the DOM by processing and generating HTML. Upon receiving a request, this server executes JavaScript to construct the DOM and sends back HTML containing all the necessary elements to the client. This transfers the page generation responsibility from the client to the server. With server side rendering our site's SEO gets restored as the SPA's pages now have fully formed HTML rather then just the container HTML, enabling better crawling by search engine bots.

However, SSR comes with a few trade-offs too. 

  • When rendering on the server side, one notable limitation is the absence of the DOM in the initial server-side render. Consequently, this absence entails a loss of essential browser utilities, such as the window object, which is typically utilized in frontend development.
  • While SSR resolves SEO concerns by providing complete HTML content for crawlers, it contradicts one of the key aspects of SPAs: reducing resource fetches by loading content asynchronously. Despite not refreshing the page in the browser, SSR still triggers server requests each time new page HTML is needed, leading to a departure from the SPA's objective of minimizing server interactions for page retrieval.

So in order to tackle these caveats, we need another rendering mechanism that enables us to handle the SPA on the client side while preventing the lose the HTML elements and SEO. Static site generation (SSG) to the rescue!

3. Static site generation (SSG)

In static site generation, HTML pages are pre-built during the application's compilation or build time rather than being generated upon request, whether from the server or the client. These fully formed HTML documents are then served to the client upon request. This approach leads to lightning-fast responses, significantly enhancing the site's performance. Additionally, leveraging caching techniques further amplifies the speed of access to these pre-generated pages. Moreover, akin to SSR, the already generated HTML documents bolster the SEO performance of the site by providing fully indexable content to search engine crawlers.

So SSG is the best, right? Well, hold on. The decision to solely rely on SSG as the ultimate rendering mechanism for SPAs requires careful consideration. While SSG offers remarkable benefits for performance and SEO preservation, it's not without its challenges. Sites that undergo frequent content updates face a notable drawback with SSG: rebuilding the entire site every time content changes can become resource intensive and time consuming task. This overhead might outweigh the advantages, especially for applications where content updates are frequent.

So if each rendering mode has its advantages and disadvantages, how do we choose the right rendering mode for our SPA? Let's discuss each separately.

Choosing the right rendering mode

For developers and product owners investing in single page applications, key considerations revolve around usability, performance, and search engine optimization.

When SEO isn't a priority for a website, opting for client-side rendering simplifies the decision-making process. Websites like SaaS products or internal organizational systems, accessed primarily by app users, often don't require SEO. In such cases, choosing CSR allows the browser to handle the rendering, minimizing server resource usage.

However, for sites that heavily rely on SEO, the decision between SSR and SSG becomes pivotal. The choice depends significantly on the content update frequency. For instance, sites featuring static content or infrequent updates, such as company information websites or personal blogs, can benefit from static site generation. SSG provides SEO benefits by serving complete HTML documents without straining the server with repeated requests for page builds.

Conversely, in scenarios where content undergoes frequent updates or involves user-generated content, the repetitive process of rebuilding with each content update proves costly. Sites like forums or platforms emphasizing user-generated content that requires SEO might find server-side rendering more suitable. SSR ensures real-time content updates without the overhead of rebuilding the entire site with every change.

Ultimately, the decision between SSR and SSG hinges on the specific needs of the website and the frequency of content updates. While SSG offers SEO advantages and fast load times for static content, SSR accommodates frequent content changes without incurring significant rebuilding costs.

Hybrid rendering approaches

1. Server to client handover

In this approach, the SPA is set up with server-side rendering, enabling the server to provide a fully formed HTML page in response to the initial request. The re-rendering or DOM manipulation responsibility is then handed over to the client after the initial render. Subsequent interactions or page updates are then managed by the client, utilizing the SPA's dynamic capabilities.

This method offers a dual advantage. For users accessing the SPA via a browser, the client takes charge after the initial request, ensuring a seamless interactive experience. Simultaneously, for search engine crawlers, each page is delivered as an individual request from the server, presenting complete HTML content and essential SEO elements. This enables better indexing and visibility for search engines, as each page's HTML content is readily available.

2. Decouple SEO-sensitive pages from the SPA

Decoupling pages that necessitate search engine optimization from the SPA is a strategy embraced by various companies, particularly within the SaaS domain. This strategy involves segregating the pages that require SEO visibility from the original application, typically operating in CSR mode. For instance, in SaaS apps, isolate non-SEO pages as a separate CSR app, while managing SEO-critical pages like marketing content or pricing details using SSR or CMS tools like Wordpress. This maintains the main app's efficiency in CSR while ensuring SEO optimization for vital pages.