top of page
  • Writer's pictureRevanth Reddy Tondapu

Part 6: Understanding Prerendering in Next.js


Understanding Prerendering in Next.js
Understanding Prerendering in Next.js

In modern web development, performance and user experience are key considerations. Next.js introduces the concept of prerendering, which differentiates it from traditional React Single Page Applications (SPAs). In this blog post, we will dive into the concept of prerendering in Next.js, focusing on the HomePage as an example.


What Is Prerendering?

Prerendering refers to the process where the Next.js server generates the HTML for each page in advance, rather than relying on JavaScript to render the content dynamically in the browser. This pre-generated HTML is then sent to the browser, which can display the page much faster.


How Prerendering Works in Next.js

Let's start by examining how the HomePage component is rendered in a Next.js application. Consider the following code snippet for the HomePage component:

export default function HomePage() {
  console.log('[HomePage] rendering');
  return (
    <>
      <h1>Indie Gamer</h1>
      <p>Welcome to the Indie Gamer website!</p>
    </>
  );
}

When you load this page, the Next.js server renders the HomePage component on the server side. You can verify this by checking the console logs. If you add a console.log statement inside the component, you'll see the message printed in the terminal where your Next.js server is running, but not in the browser console.


Inspecting Network Requests

To understand this better, let's look at the network traffic using Chrome Developer Tools:

  1. Open the Developer Tools (F12 or right-click > Inspect).

  2. Navigate to the Network tab.

  3. Reload the HomePage.

You'll notice that the browser sends an HTTP request to the server, which responds with an HTML document. This HTML includes all the elements from the HomePage component as well as any elements defined in the RootLayout.

<!DOCTYPE html>
<html lang="en">
  <head>
    <!-- Metadata and links -->
  </head>
  <body>
    <header>[Header]</header>
    <main>
      <h1>Indie Gamer</h1>
      <p>Welcome to the Indie Gamer website!</p>
    </main>
    <footer>[Footer]</footer>
  </body>
</html>

Prerendering vs. Client-Side Rendering

To highlight the difference, let's compare this with a traditional React SPA. In a typical React SPA set up with Create React App, the initial HTML is minimal and contains an empty div where the React application is mounted.

<!DOCTYPE html>
<html lang="en">
  <head>
    <!-- Metadata and links -->
  </head>
  <body>
    <div id="root"></div>
    <script src="/static/js/bundle.js"></script>
  </body>
</html>

The React application then uses JavaScript to dynamically render the components inside this div. This can be confirmed by disabling JavaScript in the browser (Settings > Disable JavaScript) and reloading the page. The React SPA will fail to render, displaying a message like "You need to enable JavaScript."

In contrast, a Next.js application will still display the pre-rendered HTML content even with JavaScript disabled. This is because the initial HTML is fully rendered on the server.


Benefits of Prerendering

  1. Faster Load Times: Since the browser receives a fully-rendered HTML document, it can display the content immediately without waiting for JavaScript to execute.

  2. SEO-Friendly: Search engines can easily index pre-rendered HTML content, improving your site's visibility and ranking.

  3. Graceful Degradation: Your site remains functional even if JavaScript is disabled or fails to load for some reason.

  4. Better Performance: Server-side rendering reduces the amount of JavaScript that needs to be executed on the client side, improving overall performance.


Combining Prerendering with Client-Side Rendering

While prerendering is beneficial, interactive functionality still requires JavaScript. Next.js balances this by using client-side rendering where necessary. For example, once the initial HTML is loaded, Next.js can dynamically update parts of the page without a full reload, providing a seamless user experience.


Conclusion

Prerendering in Next.js provides a significant advantage over traditional React SPAs by delivering faster load times, better SEO, and improved performance. By pre-rendering HTML on the server, Next.js ensures that your application is fast, reliable, and accessible.

In the next post, we will explore React Server Components, which further enhance the capabilities of server-side rendering in Next.js. Stay tuned!


18 views0 comments

Comments


bottom of page