Efficient Rendering Strategies For Smooth Websites
In the world of web development, there are many different ways of building a website or web application. With the introduction of Javascript (back in 1996), the possibilities of HTML were expanded. Shortly after web pages appeared, it became clear that something more was needed than the few features of HTML, which only provided elements that acted exclusively on the text and its style, but did not allow interactivity within the page.
One key decision to make when building a website is whether to use client-side rendering, server-side rendering, or static site generation. In this article, we will compare and contrast these three different approaches so that you can make an informed decision about which one is right for your next project.
Let’s start with some background
The web has come a long way since its humble beginnings. In the early days, web pages were static, and content was limited to simple text, styles and images.
Over time, the technology evolved and web pages became more interactive, with the addition of Ajax, and later WebSockets. Web pages could now be generated, not only on the server side but on the client side, making it possible to create complex applications that could be updated in real-time.
We face more complex problems since the user expects a unique and performant experience. This has led the technologies associated with the web to reinvent themselves every day, giving rise not only to powerful web technologies but also to more complex processing paradigms.
When a user accesses a web page, they are not only downloading HTML files, but they are also downloading pieces of code that provide interactivity to that HTML. In the end, everything will be HTML code, but that transformation from code to HTML is known as rendering.
Today there are different ways to render a website and each of them has its advantages and disadvantages. The forms of rendering currently used are Client-Side Rendering (CSR) and Server-Side Rendering (SSR) which happens when the user accesses the website and Static-Site Generation which happens when the website is deployed.
We are going to break down and analyze each of them, to understand what are the advantages and disadvantages of each of these methods.
Server Side Rendering (SSR)
In this case, SSR is the process of rendering dynamic HTML on the server. This rendering method is the oldest of all and the one used in the 90s and early 2000s.
With SSR the user makes a request to the server and the server processes the code on-demand, delivering the web page to the user’s browser. We have to take into consideration that the server is serving the page on demand every single time, so the browser is constantly making requests to the server for every new page, and each request.
The main advantage of SSR is that it can improve the performance of your application because the browser does not have to wait for JavaScript to be downloaded and executed before it can start rendering the page.
Another big advantage of SSR is that improves SEO because search engines can index the full content of the page, not just the initial HTML file.
The main disadvantage of SSR is that performs badly when we need interactivity due to the need to always request every bit of HTML to the server. In addition, SSR can sometimes result in "empty" pages if the server is slow.
Static Site Generation
Static site generation is a type of server-side rendering that is not dynamic and has the HTML code already generated in the server. This means that all of the work is done upfront before the user even visits your site.
There are several benefits to using static site generation. First, it is much faster than client-side or server-side rendering because the HTML files are already built and ready to go. Second, it is more secure because there is no dynamic code that can be exploited by hackers. Finally, it is easier to scale because you can rely on a CDN and you don't need to worry about server capacity when traffic spikes.
The downside of static site generation is that if you have a large page it can be more difficult to set up and manage than other rendering methods. Additionally, if you need to update your content frequently, you will need to rebuild and redeploy your entire website each time.
Client Side Rendering
Client-side rendering became popular in recent years with JavaScript frameworks, which enabled the creation of rich and interactive experiences. JavaScript is used to load the content of web pages. Instead of loading HTML from the web, it is loaded dynamically through Javascript, making them more responsive.
In this type of rendering, the client is responsible for processing all the Javascript code, so the first load will be expensive in terms of performance. This will result in waiting times for the user, and pages that seem to load slowly the first time they are accessed. Besides, it affects search engine robots because it slows down search engines as they crawl on the website.
On the other hand, this type of rendering does not need any special or complex server. In this case, the only function of the server is to provide the code to the clients. As we can sense, this rendering technique transfers all responsibility for processing the web page to the client.
Client-side rendering is used in highly interactive websites like Google Maps or Trello.
Hybrid approaches
Currently, there are approaches that are using both techniques in conjunction to get the best of both worlds. This results in a first load and SEO performant due to the HTML being in place from the first moment and smooth interactivity thanks to using Javascript once the page is loaded.
The emergence of frameworks that combine those hybrid approaches is now a reality. Next.js, Remix, Nuxt or SvelteKit are good examples of this and perfect tools to get the best of both techniques. These frameworks make using hybrid approaches very seamless and allow us to fine-tune the rendering strategies based on the website's needs.
When to use each of them
When we have to decide between using client-side rendering or server-side rendering there are a few key factors to consider. These include the amount of traffic the site is likely to receive, the SEO relevance and the need for real-time updates.
For high traffic levels or when SEO is important, server-side rendering may be the best option as it can reduce the amount of time needed to load pages. However, this approach does require more resources and can be more expensive to set up and maintain.
Client-side rendering can be a good option if the content is highly interactive. Can be less expensive to maintain. However, it is important to note that this approach can result in poorer SEO results.
Hybrid approaches are a good option for SEO and interactivity. In this case, you are the one with a lever and you need to evaluate when to use each approach to get the best experience for your website.
Founding member of The Crafters Lab
Àlvar is a software developer and founding member of The Crafters Lab.