SSR(Server Side Rendering)

SSR(Server Side Rendering)

A glimpse of how SSR works in Next.js

How does Nextjs work on SSR backstage?

Next.js uses a combination of server-side rendering (SSR) and client-side rendering (CSR) techniques to achieve its seamless SSR capabilities. Let's explore how Next.js works behind the scenes to implement SSR:

  1. Server Configuration: When you start a Next.js project, the framework provides a built-in server that listens for incoming requests. This server handles routing and rendering based on the requested URLs.

  2. Pages and Routes: Next.js leverages the pages directory structure. Each file in the pages directory corresponds to a route in your application. For example, a file named about.js inside the pages directory will be associated with the route /about.

  3. Server-Side Rendering (getServerSideProps): When a user navigates to a route, Next.js checks if there's a corresponding getServerSideProps function exported from the page component. If present, this function is executed on the server before rendering the page. It can fetch data from APIs, databases, or perform any other server-side tasks.

  4. Rendering Process: After fetching the data in the getServerSideProps function, Next.js renders the React component associated with the requested route. The server generates the HTML markup for the page, including the fetched data, and sends this pre-rendered HTML to the client's browser.

  5. Client-Side Hydration: Once the pre-rendered HTML is delivered to the browser, Next.js automatically takes over. It loads the necessary JavaScript code and event handlers to make the page interactive. This process is known as "hydration." The JavaScript code is responsible for handling client-side routing, interactivity, and dynamic updates.

  6. Client-Side Rendering (CSR) and Navigation: After hydration, client-side rendering takes over. When users navigate to other routes within your Next.js application, the client-side routing system comes into play. Subsequent page transitions use CSR, where only the necessary components and data are updated without a full page reload. This enhances the user experience and responsiveness of the application.

  7. Differences Between SSR and CSR: The key difference between SSR and CSR lies in where the initial rendering takes place. SSR renders the page on the server and sends pre-rendered HTML to the browser, which helps improve initial page load speed and SEO. CSR, on the other hand, renders the page in the browser using JavaScript after fetching data from APIs, which enables dynamic content and interactivity.

  8. Automatic Code Splitting: Next.js also employs automatic code splitting, meaning it intelligently separates JavaScript code into smaller chunks that are loaded only when needed. This reduces the initial payload size and optimizes the loading speed of your application.

Overall, Next.js abstracts much of the complexity of implementing SSR, client-side rendering, and code splitting, making it easier for developers to create fast, SEO-friendly, and interactive web applications without having to manage these intricacies manually.