Next.js: Powering Modern Websites

Parikshit Gehlaut
4 min readJan 19, 2024

--

In the ever-evolving landscape of web development, staying ahead of the curve is essential. Next.js, a React-based framework, has emerged as a powerful tool that simplifies and supercharges the development of modern web applications. In this blog post, we’ll explore what Next.js is, how it transforms the web experience, and delve into its key features, including server-side rendering (SSR), client-side rendering (CSR), server actions, Next Link, image optimization, app routing, and data fetching with SWR (stale-while-revalidate) and mutations.

What is Next.js?

Next.js is a React framework that aims to make web development more accessible and efficient by providing a streamlined and opinionated approach to building web applications. Developed and maintained by Vercel, Next.js combines the simplicity of React with additional features and conventions to enhance the developer experience.

I built my own portfolio website using Next.js 14 and deployed it on Vercel.

Commands to Create Next.js website:

yarn create next-app // install yarn using "npm install yarn"

On installation, you’ll see the following prompts:

What is your project named? my-app
Would you like to use TypeScript? No / Yes
Would you like to use ESLint? No / Yes
Would you like to use Tailwind CSS? No / Yes
Would you like to use `src/` directory? No / Yes
Would you like to use App Router? (recommended) No / Yes
Would you like to customize the default import alias (@/*)? No / Yes
What import alias would you like configured? @/*

Select required dev dependencies.

yarn run dev // to run your next.js application on localhost:3000

Transforming Web Experience

1. Server-Side Rendering (SSR) vs. Client-Side Rendering (CSR)

One of the key features of Next.js is its ability to perform server-side rendering. SSR generates HTML on the server side before sending it to the client, resulting in faster initial page loads and improved SEO. In contrast, CSR generates HTML on the client side using JavaScript, which can lead to slower initial page loads.

Next.js allows developers to choose between SSR and CSR based on the specific requirements of their application. This flexibility empowers developers to optimize performance and user experience.

Pros of Server Side Rendering :

  • Initial Page load is faster.
  • Better SEO.
  • Better for old Devices and slow internet connection.

Pros of Client Side Rendering :

  • Better Performance after initial page load.
  • More Interaction with users
  • Use React hooks after adding “use client” at the top of .jsx or .tsx file.

2. Server Actions

Server actions in Next.js enable developers to execute code on the server side during the build process. This feature is particularly useful for tasks like fetching data from an external API or populating a database before deploying the application. By offloading certain operations to the server, Next.js ensures that the client receives a fully prepared application.

3. Next Link

Navigating between pages in a web application is a fundamental aspect of user experience. Next.js provides the `Link` component, simplifying client-side navigation without a full page reload. This enhances the perceived speed of the application and contributes to a smoother user experience.

import Link from 'next/link'

function Home() {
return (
<ul>
<li>
<Link href="/">Home</Link>
</li>
<li>
<Link href="/about">About Us</Link>
</li>
<li>
<Link href="/blog/hello-world">Blog Post</Link>
</li>
</ul>
)
}

export default Home

4. Next Image Optimization

Image optimization is crucial for improving web performance. Next.js introduces the `next/image` component, which handles image loading, lazy loading, and image optimization out of the box. This not only simplifies the process for developers but also ensures that images are delivered efficiently to end-users.

import Image from 'next/image'

export default function Page() {
return (
<Image
src="/profile.png"
width={500}
height={500}
alt="Picture of the author"
/>
)
}

5. Next App Router

The Next.js router simplifies the process of defining and navigating between routes in a web application. With a file-based routing system, developers can organize their project structure in a way that aligns with the application’s structure, making it more intuitive and maintainable.

6. SWR (Stale-While-Revalidate) and Mutations

Next.js integrates with SWR, a powerful data fetching library, to streamline the process of fetching data in the client. SWR enables developers to implement caching strategies, such as stale-while-revalidate, improving the responsiveness of data-driven applications. Additionally, Next.js supports mutations, allowing developers to update data on the server and automatically revalidate the cache.

Conclusion:

Next.js has become a game-changer in the world of web development, offering a powerful combination of React simplicity and additional features that enhance the development experience. Whether you’re focused on server-side rendering, client-side rendering, or a mix of both, Next.js provides the tools and conventions to create modern and performant web applications. By leveraging features like server actions, Next Link, image optimization, app routing, and SWR mutations, developers can build robust and user-friendly applications that meet the demands of today’s dynamic web landscape.

--

--