Next.js Essentials
Next.js is a React framework for building fast, scalable, and production-ready web applications. You will learn about routing, data fetching, server and client components, API routes, and optimization techniques for modern full-stack development.
Next.js automatically creates routes based on your folder structure inside the app or pages directory. Each file corresponds to a route, making navigation intuitive and simple.
app/├── page.tsx → Renders the home page (/)├── about/│ └── page.tsx → Renders the About page (/about)└── blog/├── page.tsx → Renders the Blog list (/blog)└── [slug]/└── page.tsx → Dynamic route for individual posts (/blog/hello-world)
// app/about/page.tsxexport default function AboutPage() {return <h1>About Us</h1>;}// app/blog/[slug]/page.tsxexport default function BlogPost({ params }) {return <h2>Post: {params.slug}</h2>;}
Next.js supports multiple data fetching strategies: Static Site Generation (SSG), Server-Side Rendering (SSR), and Client-Side Fetching. This flexibility allows you to balance performance and freshness.
// Server Component (SSR Example)// app/posts/page.tsxexport default async function PostsPage() {const res = await fetch("https://jsonplaceholder.typicode.com/posts");const posts = await res.json();return (<div><h1 className="text-2xl font-bold mb-4">Blog Posts</h1><ul className="list-disc pl-6 space-y-2">{posts.slice(0, 5).map((post) => (<li key={post.id}>{post.title}</li>))}</ul></div>);}
// Static Generation Example// app/page.tsxexport const revalidate = 60; // Revalidate every 60 secondsexport default async function HomePage() {const res = await fetch("https://api.example.com/data", { next: { revalidate: 60 } });const data = await res.json();return <div>Pre-rendered content with revalidation.</div>;}
In Next.js 13+, components are Server Components by default. You can opt into Client Components by adding the use clientdirective. Use Client Components for interactivity and hooks.
// app/components/Counter.tsx'use client'import { useState } from "react";export default function Counter() {const [count, setCount] = useState(0);return (<div className="space-y-2"><h2>Count: {count}</h2><buttononClick={() => setCount(count + 1)}className="px-3 py-1 bg-blue-500 text-white rounded">Increase</button></div>);}// app/page.tsximport Counter from "./components/Counter";export default function Home() {return (<div className="p-6"><h1 className="text-xl font-semibold mb-4">Welcome to Next.js</h1><Counter /></div>);}
API routes let you build backend functionality directly inside your Next.js app. These run as serverless functions, perfect for handling form submissions, fetching data, or processing requests.
// app/api/hello/route.tsexport async function GET() {return Response.json({ message: "Hello from Next.js API!" });}// Example fetch from a pageexport default async function ApiExample() {const res = await fetch("/api/hello");const data = await res.json();return <p>{data.message}</p>;}
Next.js offers built-in optimization for images, scripts, and fonts. Use the next/image and next/font components to automatically improve performance.
import Image from "next/image";import { Inter } from "next/font/google";const inter = Inter({ subsets: ["latin"] });export default function OptimizedPage() {return (<div className={`${inter.className} p-6`}><h1 className="text-2xl font-bold mb-4">Optimized Example</h1><Imagesrc="/nextjs-logo.png"alt="Next.js Logo"width={150}height={150}priorityclassName="rounded"/><p>Fast and optimized by default.</p></div>);}