Why Next.js comes in next
LATBS Edition 16

Why Next.js comes in next

Written by: Pravesh Chapagain , Software Engineer


In web development, doing things right and doing things fast matter. As we know, the de facto choice of the frontend libraries has always been React. However, despite being customizable, React is slow.

Enter: NextJS—a React framework that fulfills what React lacks. Curious about how it does that? Read this blog to find out.

Next.js - A versatile framework

Next.js stands out as a true polymath. Is it merely a library, a comprehensive framework, or perhaps a sorcerer skilled in weaving code-splitting spells? It encompasses all these facets and beyond. Next.js emerges as a chameleon among frameworks, seamlessly transitioning between the elegance of a library and the potency of a framework.

Moreover, one of Next.js's most compelling features is its stellar performance, especially when compared to React alone. While React is renowned for its efficiency in building interactive user interfaces, Next.js takes it a step further. By incorporating server-side rendering (SSR), static site generation (SSG), and optimized code-splitting capabilities, Next.js ensures lightning-fast page loads and superior runtime performance. This performance boost not only enhances user experience but also contributes to improved search engine optimization (SEO) and scalability of web applications.

SSR and SSG - The dynamic duo

Unlike React where Client Side Rendering (CSR) hampers performance, Next.js uses SSR and SSG as a dynamic duo to enhance it.

SSR, as the name suggests, involves rendering web pages on the server before sending them to the client's browser. Here's how it works:

  • On-demand rendering: When a user requests a page, the server dynamically generates the HTML content for that page, including any data fetching or processing required.
  • Dynamic updates: With SSR, pages can be dynamically updated on each request, ensuring that users receive the latest content and data.
  • SEO friendliness: SSR is highly advantageous for search engine optimization (SEO) purposes, as search engine crawlers can easily parse and index the server-rendered HTML content.
  • Complex rendering logic: SSR is ideal for pages with dynamic or personalized content that cannot be determined at build time. It allows for the execution of server-side logic, such as authentication, before rendering the page.

SSG, on the other hand, involves pre-rendering entire pages at build time, resulting in static HTML files that are served to clients without the need for server-side processing during runtime. Here's a closer look:

  • Pre-generated pages: During the build process, Next.js generates HTML pages for all routes in advance, based on the content available at build time.
  • Improved performance: SSG offers blazing-fast page loads since the pre-rendered HTML can be served directly from a content delivery network (CDN) without any server processing.
  • Cacheability: The static HTML files produced by SSG are highly cacheable, leading to improved scalability and reduced server load.
  • Great for content sites: SSG is particularly well-suited for content-driven websites where the content changes infrequently or can be updated periodically.

Now, what should we choose? Here are some pointers to get you started:

  • SSR: Choose SSR when your pages require dynamic content that cannot be determined at build time, or when real-time data fetching and processing are necessary.
  • SSG: Opt for SSG when your content is relatively static or updates infrequently, as it offers superior performance, scalability, and simplicity.

File based routing

We know how tiring a React router can be to manage routes. Creating routes and adding them on different pages, and then adding the global routes can be somewhat cumbersome.

Next.js, on the contrary, uses file-based routing. In part, making files and keeping it in the ‘pages’ folder automatically creates routes for you on the fly. Here is the folder structure for pages in NextJS.

Here, index.js is the main entry point and will serve as the landing page. Other pages will serve as their filename in the lower kebab case. If you want to serve dynamic routes which include some parameter, you can create a folder with the name of the route and create a js file with the name of the parameter enclosed in brackets.

Code splitting mastermind

It’s a known fact that code splitting makes websites load faster. While React supports code splitting, unlike Next.js, it has to be implemented by the developer using tools like webpack.

On the other hand, Next.js offers built-in support for code splitting, making it easy to implement this optimization technique in your applications.

Here's how code splitting works in Next.js:

  1. Automatic code splitting: Next.js automatically splits code based on page boundaries. Each page in your Next.js application becomes a separate entry point, resulting in automatically generated bundles.
  2. Component-level splitting: In addition to page-level code splitting, Next.js allows you to split code at the component level using dynamic imports. This enables you to lazy-load components, libraries, or modules only when they're needed, further reducing initial bundle size.
  3. Optimized loading: Next.js optimizes the loading of split code by prefetching resources during navigation. This ensures that code is available when it's needed, without compromising performance.

Building serverless frontend applications

Since React is primarily focused on the client-side, it doesn't have built-in support for creating API routes. If you need to make API requests in React, you typically use libraries like ‘fetch’ to communicate with a backend server or external APIs.

However, creating API routes in Next.js is straightforward and integrated into the framework. API routes in Next.js allow you to create serverless functions that handle HTTP requests. These functions are executed server-side, enabling you to define custom API endpoints without the need for a separate backend server.

Creating API Routes

Creating API routes in Next.js is simple. Simply create a file inside the pages/api directory with the desired endpoint name, and export a function that handles the request. Here's an example:

Benefits of API Routes

API routes offer several benefits for Next.js developers:

  • Simplified backend development: With API routes, you can build backend services directly within your Next.js application, eliminating the need for a separate backend server.
  • Integrated deployment: API routes are seamlessly integrated into the Next.js deployment process, ensuring that they are automatically deployed alongside your frontend code.
  • Efficient scaling: API routes benefit from the scalability and performance optimizations provided by Next.js, allowing your backend services to handle high traffic loads efficiently.

Next.js - More Than a Framework

Next.js isn't just a framework—it’s a jack of all trades. With modularity, scalability, and a dash of creativity, you're not just building apps but creating digital masterpieces.

So, the next time you want to create an application, give Next.js a try. It's fast and lets you create your masterpiece in no time, and with blazing fast performance to boot.

Pukar Sharma

Full stacks Dev at Hyperce (Ecommerce Suite Partner)

8mo

If performance and dx is priority, maybe one can try sveltekit. 😇 No wierd hydration issues plus reactivity out of the box.

Abhinas Regmi

Full Stack Developer | Computer Engineer (Student)

8mo

Next ko middleware ko barema k opinion xa hajur ko?? Edge ma matra run hune huda kei problem awnxa hola pachi. Is it good idea to use middleware in nextjs vendor? lock hunxa bhanne ni suneko xu.

To view or add a comment, sign in

More articles by Leapfrog Technology, Inc.

Insights from the community

Others also viewed

Explore topics