Back to Home
    Next.jsNext.js v15Intermediate

    Next.js

    Next.js App Router, server components, data fetching, routing, metadata, and deployment patterns.

    12 min read
    nextjsreactfullstackssr

    App Router

    2 topics

    File-based Routing

    bash
    app/
    ├── layout.tsx          # Root layout (wraps all pages)
    ├── page.tsx            # / route
    ├── loading.tsx         # Suspense fallback
    ├── error.tsx           # Error boundary
    ├── not-found.tsx       # 404 page
    ├── (marketing)/        # Route group (no URL segment)
    │   └── about/
    │       └── page.tsx    # /about
    ├── blog/
    │   ├── page.tsx        # /blog
    │   └── [slug]/
    │       └── page.tsx    # /blog/:slug
    └── api/
        └── users/
            └── route.ts    # API route: GET/POST /api/users

    💡 Route groups (marketing) let you share layouts without affecting the URL

    ⚡ loading.tsx automatically wraps the page in a Suspense boundary

    Server vs Client Components

    tsx
    // Server Component (default — runs on server, 0 JS sent)
    export default async function UserList() {
      const users = await db.users.findMany(); // Direct DB access!
      return (
        <ul>
          {users.map(u => <li key={u.id}>{u.name}</li>)}
        </ul>
      );
    }
    
    // Client Component (needs interactivity)
    "use client";
    import { useState } from "react";
    
    export function Counter() {
      const [count, setCount] = useState(0);
      return <button onClick={() => setCount(c => c+1)}>{count}</button>;
    }
    
    // Pattern: server parent, client leaf
    // UserList (server) → renders <Counter /> (client)

    💡 Default is Server Component — only add 'use client' when you need hooks/events

    ⚡ Push 'use client' as far down the tree as possible to maximize server rendering

    Data Fetching

    1 topic

    fetch & caching

    tsx
    // Static (cached at build time)
    const data = await fetch("https://api.example.com/posts");
    
    // Revalidate every 60 seconds
    const data = await fetch("https://api.example.com/posts", {
      next: { revalidate: 60 }
    });
    
    // No cache (always fresh)
    const data = await fetch("https://api.example.com/posts", {
      cache: "no-store"
    });
    
    // Tag-based revalidation
    const data = await fetch("https://api.example.com/posts", {
      next: { tags: ["posts"] }
    });
    
    // Revalidate from Server Action
    import { revalidateTag } from "next/cache";
    revalidateTag("posts");

    ⚡ Tag-based revalidation is the best pattern — revalidate exactly what changed

    Server Actions

    1 topic

    Forms & Mutations

    tsx
    // app/actions.ts
    "use server";
    import { revalidatePath } from "next/cache";
    
    export async function createPost(formData: FormData) {
      const title = formData.get("title") as string;
      await db.posts.create({ data: { title } });
      revalidatePath("/blog");
    }
    
    // app/new-post/page.tsx
    import { createPost } from "../actions";
    
    export default function NewPostPage() {
      return (
        <form action={createPost}>
          <input name="title" required />
          <button type="submit">Create</button>
        </form>
      );
    }

    💡 Server Actions work without JavaScript — they're regular form submissions

    ⚡ Always validate/sanitize input in Server Actions — they're public API endpoints

    Related Articles

    Background reading and deeper explanations for this sheet.

    Build an Agentic App with FastAPI and Azure AI Foundry: A Practical Beginner-to-Pro Guide

    Learn how to design, build, and deploy an agentic application using FastAPI and Azure AI Foundry with practical, real-world examples. This guide walks you from architecture and setup to tool-calling, memory, observability, and production deployment patterns.

    keyword overlap

    GenAI SaaS Architecture: A Practical Blueprint for Building, Scaling, and Securing AI Products

    Designing a SaaS product on top of GenAI is more than calling an LLM API—it requires thoughtful architecture across product, data, safety, and operations. This practical guide walks you through a beginner-friendly yet deep system blueprint, with real-world patterns, code snippets, and deployment strategies you can use immediately.

    keyword overlap

    Intro to FastAPI: Build a Production-Ready Python API with Real Examples

    FastAPI is one of the fastest ways to build modern APIs in Python without sacrificing code quality or developer experience. In this practical, beginner-friendly guide, you’ll learn how to create endpoints, validate data, handle errors, connect a database, secure routes, and prepare your FastAPI app for real-world deployment.

    keyword overlap

    Create a Generative Email Triage App with FastAPI, Postgres, LangGraph, and Azure Foundry

    Learn how to build a production-ready generative email triage app using FastAPI, Postgres, LangGraph, and Azure Foundry. This guide walks you through architecture, data modeling, workflow orchestration, and practical implementation details with real-world examples. By the end, you’ll have a beginner-friendly but deep blueprint you can run, extend, and deploy.

    keyword overlap