What is Dynamic Rendering? Guide to Setting Up Dynamic Rendering for CSR Pages with Nginx

What is Dynamic Rendering? Guide to Setting Up Dynamic Rendering for CSR Pages with Nginx

Daily short news for you
  • A software that converts text to speech created by a Vietnamese programmer - J2TEAM - Text to Speech (Free). You can convert dozens of languages into dozens of different natural voices. The special thing is that it is free.

    In preliminary evaluation, the conversion of long texts or texts in pure Vietnamese is very good. However, when it includes English words, it sounds a bit funny 😅

    » Read more
  • How terrifying, Codeium - known as a competitor to Github Copilot, as it allows users to use it for free without limits. Recently, they introduced the Windsurf Editor - no longer just a VSCode Extension but a full Editor now - directly competing with Cursor. And the main point is that it... is completely free 🫣.

    » Read more
  • There is a rather interesting study that I came across: "Users never bother to read things they don't want to." (That's a bold statement, but it's more true than not. 😅)

    Don't believe it? I bet you've encountered situations where you've clicked on a button repeatedly and it doesn't respond, but in reality, it has displayed an error message somewhere. Or you've filled out everything and then when you hit the submit button, it doesn't go through. Frustrated, you scroll up or down to read and find out... oh, it turns out there's an extra step or two you need to take, right?

    It’s not far from the blog here. I thought that anyone who cares about the blog would click on the "Allow notifications" button just below the post. But the truth is, no one bothers to click it. Is it because they don't want to receive notifications? Probably not! I think it's because they just didn’t read that line.

    The evidence is that only when a notification pops up and takes up half the screen, or suddenly appears to grab attention, do they actually read it—and of course, it attracts a few more subscribers—something that was never achieved before.

    » Read more

The Problem

In recent years, the explosion of UX/UI Frameworks like Angular or React has revolutionized web development, focusing on user interactions and completely browser-based UI processing.

These types of websites are called Client Side Rendering (CSR) and are commonly implemented as Single Page Applications (SPA). They leverage the processing power of user devices to reduce the load on servers. Moreover, these websites provide a smooth user experience by only requiring the server to send JavaScript code to the device, and fetching necessary data through APIs instead of loading the entire HTML code.

While CSR excels in certain aspects, it is not SEO-friendly. For those unfamiliar with SEO, it is the practice of making a website discoverable on the internet through search engines like Google or Bing. These search engines continuously send "crawlers" to visit all known websites and read their content. While these "crawlers" can read HTML well, they struggle with JavaScript. Therefore, making CSR websites SEO-friendly is crucial.

To achieve this, there are several methods available, such as using Server Side Rendering (SSR) libraries or completely building websites as static HTML - Server Side Generation (SSG). Each method has its pros and cons, and the choice depends on the specific use case. However, in this article, we will not delve into these two methods.

Instead, Google has mentioned a solution for SEO-friendly SPA websites called Dynamic Rendering. What is it exactly?

![Dynamic Rendering](https://img.youtube.com/vi/CrzUP6MmBW4/0.jpg)

What is Dynamic Rendering?

Dynamic Rendering is a term that refers to delivering query results by distinguishing between regular users and "crawlers" from search engines. If it's a regular user, JavaScript code is returned, but if it's a "crawler", an HTML page is returned.

Dynamic Rendering

Because "crawlers" are primarily interested in the content of your website, returning HTML results is essential for them to gather information. On the other hand, regular users not only care about the content but also the UI/UX, as they actively interact with the elements displayed on the website.

To achieve this, we rely on the User-Agent string of the user's web browser. Since User-Agent strings can differentiate between users and search engine "crawlers", we can return the desired results based on this information. The process of distinguishing between users and "crawlers" can be done at the HTTP server level using tools like Nginx, or if you are using frameworks like Nuxt or Next, they provide configurations that make this setup easy.

Currently, there are various ways to implement Dynamic Rendering. We can seek third-party services like Prerender.io or build our own HTML-rendering service using Google Chrome's Rendertron library.

Self-deploying Rendering Support Services

Rendertron is a headless Chrome, designed to render and regenerate web pages quickly.

It may sound interesting to have a browser without a user interface. After all, how can we browse the web without one? The reason is that it is not designed for regular web browsing. Removing display components allows the browser to be lightweight and more flexible in a server environment.

Rendertron provides various methods to create a rendering server. Let's assume we are running a Rendertron server using the Express.js module of Node:

const express = require('express');
const rendertron = require('rendertron-middleware');

const app = express();

app.use(
  rendertron.makeMiddleware({
    proxyUrl: 'http://my-rendertron-instance/render',  
  })
);

app.use(express.static('files'));
app.listen(8080);

Replace http://my-rendertron-instance with the server address you set up. You can find detailed configuration instructions here.

Afterward, try accessing the address http://my-rendertron-instance/render/your-spa-url with your-spa-url being the address of your CSR page. You should see the rendered HTML code of the web page returned.

Configuring Dynamic Rendering for CSR Pages with Nginx

If you are deploying a CSR website through Nginx, here is a guide for you.

First, we need to determine a list of "crawler" User-Agent strings:

map $http_user_agent $is_bot {
        default                                     0;
        "~*Prerender"                               0;
        "~*googlebot"                               1;
        "~*yahoo!\ slurp"                           1;
        "~*bingbot"                                 1;
        "~*yandex"                                  1;
        "~*baiduspider"                             1;
        "~*facebookexternalhit"                     1;
        "~*twitterbot"                              1;
        "~*rogerbot"                                1;
        "~*linkedinbot"                             1;
        "~*embedly"                                 1;
        "~*quora\ link\ preview"                    1;
        "~*showyoubot"                              1;
        "~*outbrain"                                1;
        "~*pinterest\/0\."                          1;
        "~*developers.google.com\/\+\/web\/snippet" 1;
        "~*slackbot"                                1;
        "~*vkshare"                                 1;
        "~*w3c_validator"                           1;
        "~*redditbot"                               1;
        "~*applebot"                                1;
        "~*whatsapp"                                1;
        "~*flipboard"                               1;
        "~*tumblr"                                  1;
        "~*bitlybot"                                1;
        "~*skypeuripreview"                         1;
        "~*nuzzel"                                  1;
        "~*discordbot"                              1;
        "~*google\ page\ speed"                     1;
        "~*qwantify"                                1;
        "~*pinterestbot"                            1;
        "~*bitrix\ link\ preview"                   1;
        "~*xing-contenttabreceiver"                 1;
        "~*chrome-lighthouse"                       1;
        "~*telegrambot"                             1;
}

Next, configure Nginx to redirect these User-Agent strings to the Rendertron server:

server {
    listen 80;
    server_name example.com;
    ...  

    if ($is_bot = 1) {
        rewrite ^(.*)$ /rendertron/$1;
    }

    location /rendertron/ {
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_pass http://my-rendertron-instance/render/$scheme://$host:$server_port$request_uri;   
    }
}

Don't forget to restart Nginx and test your website using a browser or tools like Postman with a "crawler" User-Agent string to see if your website returns the HTML code.

Here is an example User-Agent string from Google:

Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Mobile Safari/537.36 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)

Conclusion

Single Page Apps are currently a popular web development trend due to the advantages they offer. However, SPAs are not SEO-friendly. Therefore, Dynamic Rendering is one of the solutions to bring SPAs closer to search engines.

Premium
Hello

Me & the desire to "play with words"

Have you tried writing? And then failed or not satisfied? At 2coffee.dev we have had a hard time with writing. Don't be discouraged, because now we have a way to help you. Click to become a member now!

Have you tried writing? And then failed or not satisfied? At 2coffee.dev we have had a hard time with writing. Don't be discouraged, because now we have a way to help you. Click to become a member now!

View all

Subscribe to receive new article notifications

or
* The summary newsletter is sent every 1-2 weeks, cancel anytime.
Author

Hello, my name is Hoai - a developer who tells stories through writing ✍️ and creating products 🚀. With many years of programming experience, I have contributed to various products that bring value to users at my workplace as well as to myself. My hobbies include reading, writing, and researching... I created this blog with the mission of delivering quality articles to the readers of 2coffee.dev.Follow me through these channels LinkedIn, Facebook, Instagram, Telegram.

Did you find this article helpful?
NoYes

Comments (1)

Leave a comment...
Avatar
Văn Mạnh2 years ago
Cách này tôi thấy tốc độ chậm đi đáng kể, nhưng ko sao bởi vì chỉ dành cho bot thôi mà :D
Reply
Avatar
Xuân Hoài Tống2 years ago
Đúng rồi bạn nhưng nếu chậm quá thì cũng không tốt đâu vì mình được biết thuật toán của GG có tính cả điểm page speed nữa nên cần lưu ý