Back to all posts
Next.jsReactWeb DevelopmentFrontend

Getting Started with Next.js: App Router, Server Components & Best Practices

March 15, 20258 min read
Getting Started with Next.js: App Router, Server Components & Best Practices

Getting Started with Next.js: App Router, Server Components & Best Practices

Next.js has revolutionized React web development. With the release of the App Router, React Server Components (RSC), and built-in optimization tools, it has become the standard for building production-ready, lightning-fast web applications.


What makes Next.js powerful?

Next.js bridges the gap between client-side flexibility and server-side performance. Unlike traditional Single Page Applications (SPAs) that send blank HTML files with heavy JS bundles, Next.js pre-renders pages on the server and streams HTML directly to the browser.

Key Benefits

  1. Zero-Config SSR & SSG: Pre-render pages at build time or request time effortlessly.
  2. React Server Components (RSC): Keep heavy dependencies server-side to shrink JavaScript bundle sizes sent to the user.
  3. Automatic Code Splitting: Load only the code required for the current view.
  4. Built-in Image & Font Optimization: Built-in next/image and next/font eliminate layout shifts (CLS) automatically.

Understanding the App Router Directory Structure

The app/ directory introduced a file-system based router that uses special filenames to define layouts, loading states, and error handling:

my-next-app/
├── app/
│   ├── layout.tsx         # Root layout wrapping all pages
│   ├── page.tsx           # Home page route (/)
│   ├── globals.css        # Global CSS styles
│   ├── blog/
│   │   ├── page.tsx       # Blog list route (/blog)
│   │   └── [slug]/
│   │       └── page.tsx   # Dynamic blog detail route (/blog/my-post)
│   └── api/
│       └── route.ts       # Backend API endpoint (/api)
├── public/                # Static assets (images, icons)
├── tailwind.config.ts
└── package.json

Server Components vs Client Components

By default, every component inside the app/ directory is a Server Component.

Server Component Example

Server components can safely query databases, read from local filesystems, or make secure API calls with secret keys directly on the server:

// app/blog/page.tsx (Server Component)
import { getPostsFromDatabase } from "@/lib/db";

export default async function BlogPage() {
  const posts = await getPostsFromDatabase();

  return (
    <main className="max-w-4xl mx-auto py-12 px-4">
      <h1 className="text-3xl font-bold mb-6">Latest Articles</h1>
      <div className="grid gap-6">
        {posts.map((post) => (
          <article key={post.id} className="p-6 bg-neutral-900 rounded-lg">
            <h2 className="text-xl font-semibold">{post.title}</h2>
            <p className="text-neutral-400 mt-2">{post.excerpt}</p>
          </article>
        ))}
      </div>
    </main>
  );
}

When to use "use client"?

Use Client Components when you need interactivity:

  • React hooks (useState, useEffect, useContext)
  • Event listeners (onClick, onChange, onScroll)
  • Browser APIs (localStorage, navigator)
"use client";

import { useState } from "react";

export default function Counter() {
  const [count, setCount] = useState(0);

  return (
    <button
      onClick={() => setCount(count + 1)}
      className="px-4 py-2 bg-purple-500 text-black font-semibold rounded-md"
    >
      Count: {count}
    </button>
  );
}

Modern Data Fetching Strategies

In the App Router, data fetching relies on standard native fetch() extended with caching and revalidation controls:

// Static Site Generation (Cached indefinitely)
const data = await fetch('https://api.example.com/data');

// Incremental Static Regeneration (Revalidate every hour)
const data = await fetch('https://api.example.com/data', {
  next: { revalidate: 3600 }
});

// Dynamic Server Rendering (No cache, fetch on every request)
const data = await fetch('https://api.example.com/data', {
  cache: 'no-store'
});

Conclusion

Next.js provides a comprehensive, production-proven framework for modern React developers. By understanding Server Components and leveraging file-system routing, you can build applications that deliver peak performance and an exceptional developer experience.

Daniel Chukwudubem

Daniel Chukwudubem

Software engineer passionate about building great products.