DEV Community

Cover image for Understanding Nextjs Rendering methods: CSR, SSR, SSG, ISR
Pauline Oraro
Pauline Oraro

Posted on

Understanding Nextjs Rendering methods: CSR, SSR, SSG, ISR

As web development continues to evolve, it is crucial for developers to grasp the different rendering approaches available so as to optimize performance and user experience. Nextjs is a popular framework that offers several rendering methods. In this article we will delve into the concepts of client side rendering, server side rendering, static site generation and incremental static regeneration.

What is rendering

Rendering is the process of converting react code into HTML. The rendering you choose will depend on the data you are working with. Rendering can take place on the server or on the client. It can happen either ahead of time at build time or on every request at runtime.

some definations:
  • Build-time: It is a name given to a series of steps that prepare your application code for production(is the process of making your application to be deployed and consumed by users).

  • Run-time: A period of time when your application runs in response to a user's request after the application is deployed.

  • Client: The browser on a user's device that sends a request to a server for your application code then turns the response into an interface the user can interact.

  • Server: A computer in a data center that stores your application code which receives request from a client. It does some computation and sends back an appropiate response.

  • CDN: Content delivery network stores static content in multiple locations around the world and places them between the client and the origin server. When a new request comes in the closest CDN can respond with cached results.

Client Side Rendering

The browser receives an empty HTML shell from the server along with the Javascript instructions to construct the UI. This is called client side rendering because the initial rendering work happens on the user's device.

Client side rendering

Benefits

  • Enhanced interactivity: Web applications can offer seamless interactivity to users. Actions such as dynamic content updates, form submissions and real-time data changes can be handled without requiring a full page reload which leads to increased user engagement.

  • High performance: Client side rendering generates on-demand HTML. It will not refresh the whole page. This saves alot of both computation power and RAM thus is gets quicker results.

Pre-rendering

By default, Nextjs pre-renders every page which means that Nextjs generates HTML for each page in advance instead of having it all done by on client-side.
Nextjs has three forms of pre-rendering; Static site generation, Server side rendering and Incremental static regeneration.

Server Side Rendering

The HTML page is generated on the server for each request. The generated HTML, JSON data and Javascript instructions to make the page interactive are then sent to the client.

Server side rendering

Benefits

  • Faster initial page load time: With server side rendering, the server sends pre-rendered HTML to the client reducing the time required to display the content. Users can see the content more quickly leading to a better first-time user experience.

  • Search engine optimization: Search engines can easily crawl and index server side rendering generated pages since they receive fully formed HTML content. This improves the discoverability of your web application and positively impacting SEO rankings.

Static Site Generation

The HTML is generated on the server but unlike server side rendering, there is no server at run-time instead content is generated once at build-time when the application is deployed and the HTML is stored in a CDN and reused for each request.

Static site generation

Benefits

  • Blazing-fast page loads: Static site generation pre-renders all the pages during the build process resulting in incredibly fast initial page load for users. As a result visitors can access content almost instantly enhancing overall user experience.

  • Caching with CDN's: Building HTML pages opens the posibility for CDN caching. The pages are stored closer to the user and hence can be accessed much faster. Every request doesn't have to wait for the server to render the page it just receives the page from the CDN.

Incremental Static Regeneration

Nextjs allows you to create or update static pages after you have built your site. Incremental static regeneration enables you to use static generation on a per-page basis without needing to rebuild the entire site.

Incremental static regeneration

Benefits

  • Personalized content: With incremental static regeneration you can generate personalized static pages based on user-specific data. You can fetch and render content tailored to users using request parameters or cookies.

  • Real-time updates: With incremental static regeneration, it has the ability to update pages dynamically and ensures users have access to the most recent data.

Conclusion
In conclusion, comprehending the various rendering methods in Nextjs is essential for any web developer seeking to optimize their projects for better performance and user experience. As you continue your journey in web development remember to carefully analyze your project's requirements and user expectations before choosing a rendering method.

Top comments (0)