So, you have to scale the server horizontally, for example, if you want to add more servers to serve your application, because you need to scale, or you need to add more power to the server you already have if you want to scale vertically. That depends, of course, on how you want to scale your applications.
So, there is a third option that we are going to explore, which is called static site generation. So, just like server side rendering, this is what you get when you request a static site generated page. So, basically as soon as you make a request, you don't need to server side render it, because you already rendered it during build time. So, you will serve your application as static files directly to the browser.
This approach has pros and cons of course, one pro is that it's super easy to scale, and that's because again, you don't need a server. You already have static assets, you put them in a CDN, on S3, on wherever you want, on a bucket, and you just serve them as static assets, because you don't need to render the page every single time you get a request. So, that leads to outstanding performances, because the page is ready, you don't need to render it either on the client or server side. So, the page is done, you just have to transfer it. It is really secure because you don't have a server, so there's no way to attack the server directly, so that's another thing to keep in consideration.
Of course, again, it has some cons. Let's see them. So, no dynamic content is allowed on the server side, because you don't have a server, so you can't render specific things for a specific user on the server, because you don't have a server at all. It relies on client-side rendering for all the dynamics part of your application. So, for example, if you are on Instagram and you're scrolling the feed, you could generate the feed page with the client-side rendering, sorry with static side generation, but eventually you will need to wait for the client to understand who is the user that is logged in and then render the content which is specific to them. And one other thing, which is really annoying, I would say, is that if you need to change something, for example, a typo on the home page, you will need to reload and rebuild and redeploy the whole application. That can be fixed with incremental static regeneration, but we will see that in detail later on.
So one nice thing about React is that you don't have to compromise, you don't have to choose one rendering strategy for the whole website. But you can do that granularly at the page, when you basically render the single page. This is called hybrid rendering. So basically you might say, okay, the home page for this website will be a statically generated page. If you are in a blog, the post page can be dynamically generated with the server-side rendering, for example. That's cool. And some parts of the application can be rendered on the client-side only. So you can choose how long basically the user has to wait before assessing the content. Let's see in detail what I mean by waiting. So for example, this is client-side rendering, right? We make a request and as soon as we make the request, the server or the CDN will send us an empty page. As soon as the JavaScript bundle containing the React application gets executed, we will start seeing the application getting mounted on the browser. So as you can see, the server will be really fast in sending a response, but we will have to wait.
Comments