Server-side rendering(SSR) with React[Part-1]
- Server-side rendering
This article is not for beginners, I'll assume that you save some experience working with React. The article consists of two parts, this is Part-1 of the article. The first part covers the fundamentals that are required to actually know how SSR with react works. We'll then move on to Part-2 where I'll explain SSR with react and also the implementation about how can we enable SSR in React and give our app a performance boost.
Read in your preferred platform:
We know how React renders our app on the client-side. We can as well render our React app at the server-side and leverage the advantages that SSR offers us. But before going any further let's have a look at what we mean by client-side and server-side rendering.
What is Client-side rendering?
If you have some experience with building react applications, by now you would be familiar with
Is there a better way to render our app? Yes! Server-side rendering to our rescue
What is Server-side rendering?
In Server-side rendering(or SSR) we load our HTML pages onto the server instead of rendering them in the browser. When we visit a website, we make an HTTP request to our server, the server sends back a fully rendered HTML page as a response that almost instantly gets rendered(also depends on the user's internet speed, location of the server, and on a multitude of factors).
This solves our issue related to initial load time. Also, Web Crawler now sees a rendered page and can index our app thereby ensuring SEO.
However, now let's look at another side of the SSR.
Though it solves the problem that we faced with client-side rendering it has its downside.
Suppose we visit a website that is server-side rendered. Our app loads fully rendered content returned by the server almost instantly as discussed. However, if we navigate to another page of our app, we make another request to our server returning us the corresponding webpage, and then the content is rendered from the ground up.
As we see that even for a small change that needs to be rendered while we navigate to another page the content is fetched and rendered from the ground up. That means with every request the entire new page would be rendered, and not just the new content. Therefore, we make frequent requests to the server, which is bad.
So, can we solve this issue of making frequent requests and avoid full page reloads? Yes! SSR with React
In this article, we saw two different ways of rendering our content i.e client-side and SSR. We also saw what are the drawbacks of both and how they solve them.
In PART-2 of this article, I'll cover SSR with React and how we can enable SSR in React.
I hope you liked the content, Part-2 is coming soon. Stay Tuned!
PART-2 is now live.🔥
If you have any suggestions or questions or found this article helpful, please let me know in the comments.
Follow me on Twitter.