codedamn logo
codedamn

Server-Side Rendering in Next js

  • Vishnupriya's profile image
    Vishnupriya
    Team codedamn
Server-Side Rendering in Next js

What is Next js?

It is a minimalistic framework used for building SPAs (Single Page Applications) with JavaScript in a customizable way. It supports SSR (Server-Side Rendering) and has high performance. This is why it is extensively used today for building large-scale complex applications in an easy way. 

What is server-side rendering?

It is mainly used for the SEO of the site. So if a higher search rank is a priority then server-side rendering should be used while developing the application. Next js provides a better solution when it comes to rendering a page in an application. For instance, if the server-side rendering of Next js is used then it allows an option of disabling and enabling server-side rendering for a specific page. In order to render the page server-side getServerSideProps is used and allows access to props of server-side.

SSR or server-side rendering is a  process of pre-rendering a page on the server according to the user’s request. After this, the JavsScript code is executed to make the page interactive as soon as the pages are pre-rendered on the browser. This process makes pages load faster. Apart from this, server-side rendering is easy to use and is preferable for building applications that require SEO.

For an application that uses server-side rendering, a full HTML document is rendered on the front end. The server reads all the data and then executes the JavaScript code. After this, the page is created and all the data is been displayed on the front-end of the application.

Initial processing of the server

It consists of looping through the server data, accessing the API, and some data functions. The servers are extremely fast and can work much faster than the user’s computer. Due to better internet connection, they have better speed, and the API response is much faster. 

The additional process can be done through servers like asynchronous requests to API and document updation without refreshing the page.

The main feature of this is that it allows fast speed in which servers are able to process the page faster and has a faster response. In server-side rendering, it usually takes some time for the application to get data from the server, but once the data is fetched, the loading is almost instant. This is because in server-side rendering, the load is on a server and hence the loading is faster. However, this still takes less time as compared to client-side rendering. 

The custom server, however, acts as a proxy, and the request is processed before Next js handles it. Middleware provides building blocks to the application and is executed before the rendering of Next js server-side rendering.

Features of Middleware:

  • Sessions
  • Authentication
  • Rate limiting
  • Enforcing authorization and authentication

 Once a session is there in the application of a user, the API endpoint and page are rendered after which authentication is required using the Next js API route. The user application sends cookies along with the request due to which information is readily available on the server-side. 

server-side rendering
Server-Side Rendering

How is server-side rendering good for SEO?

Well, we covered how server-side works at the backend. When an application or website uses server-side rendering then all of the information is returned to the browser. This is because the search engine crawls the application’s page. The data or the information is returned to the front-end. Every second of rendering on the client-side can cost on-page SEO. The application gets crawled as soon as the HTML document is rendered on the client-side from the server. Thus, with Next js, all the information is already pre-loaded on the front-end or the search engine. 

On the other hand, if the same thing is done using some other framework like React, the basic HTML document is loaded first after that the information on the page is fetched and displayed. Due to this, the crawling happens early and information is fetched later which gives it a low SEO score.

Conclusion

This was about Server Side Rendering in Next.js. If you have any queries or feedback do let us know in the comment section. If you are interested in learning React, do check out courses on codedamn with an in-built environment playground to learn and practice code. Join the community of codedamn and do check out other articles related to programming.

User avatar