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:
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:
Now, what should we choose? Here are some pointers to get you started:
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.
Recommended by LinkedIn
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:
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:
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.
Full stacks Dev at Hyperce (Ecommerce Suite Partner)
8moIf performance and dx is priority, maybe one can try sveltekit. 😇 No wierd hydration issues plus reactivity out of the box.
Full Stack Developer | Computer Engineer (Student)
8moNext 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.