Next.js Basics
Server-side rendering, the App Router, data fetching, and why Next.js is the React meta-framework.
What you'll learn
- What Next.js is and how it extends React
- Server-side rendering and static generation
- The App Router and file-based routing
- Server and client components
Concept
What Is Next.js?
Next.js is a React meta-framework built by Vercel. It extends React with server-side rendering (SSR), static site generation (SSG), file-based routing, API routes, image optimization, and more. While plain React is a client-side library, Next.js gives you a full-stack framework with sensible defaults. It is the recommended way to start new React projects (the React team itself recommends Next.js for new apps). Next.js handles the build tooling, routing, and rendering strategy so you can focus on building features.
Rendering Strategies
Next.js supports multiple rendering strategies per page. Static Site Generation (SSG) pre-renders pages at build time — the fastest option, ideal for blogs and marketing pages. Server-Side Rendering (SSR) renders on the server on each request — ideal for personalized, frequently changing content. Incremental Static Regeneration (ISR) regenerates static pages in the background at a specified interval — a hybrid approach. In the App Router (Next.js 13+), components are server components by default, rendering on the server with zero client JavaScript.
The App Router
Next.js 13 introduced the App Router, which uses the app/ directory for file-based routing. Each folder is a route segment, and page.tsx defines the UI for that route. layout.tsx defines shared layouts that wrap child pages. loading.tsx shows a fallback during navigation. error.tsx handles errors. This file convention eliminates the need for a separate routing configuration — the file system IS the router. Nested layouts and parallel routes are powerful features of the App Router.
Server and Client Components
In the App Router, components are server components by default — they render on the server, cannot use hooks or browser APIs, and send zero JavaScript to the client. To make a component a client component, add the "use client" directive at the top. Client components can use hooks, event handlers, and browser APIs. This split lets you ship less JavaScript: most of your page can be server components, with small interactive islands as client components. This is a fundamental shift from traditional React where everything runs in the browser.
Example
// app/page.tsx — Server Component (default in App Router).
// Runs on the server, can fetch data directly, sends zero JS to client.
async function HomePage() {
// Direct data fetching on the server — no useEffect needed.
const res = await fetch("https://api.example.com/posts", {
next: { revalidate: 60 }, // ISR: revalidate every 60 seconds.
});
const posts = await res.json();
return (
<main>
<h1>Latest Posts</h1>
{posts.map(post => (
<article key={post.id}>
<h2>{post.title}</h2>
<p>{post.excerpt}</p>
<LikeButton postId={post.id} /> {/* Client component island */}
</article>
))}
</main>
);
}
// app/components/LikeButton.tsx — Client Component.
"use client"; // This directive makes it a client component.
import { useState } from "react";
export function LikeButton({ postId }: { postId: number }) {
const [liked, setLiked] = useState(false);
return (
<button onClick={() => setLiked(!liked)}>
{liked ? "♥ Liked" : "♡ Like"}
</button>
);
}
HomePage is a server component that fetches data directly on the server. LikeButton is a client component (use client) that needs interactivity. Only LikeButton ships JavaScript to the browser.
Try it
- Environment File Generator
Generate environment variables for Next.js server and client configs.
- HTTP Status Codes
Understand status codes for Next.js API routes and SSR responses.
Common mistakes
The mistake
Adding 'use client' to every component by default
The fix
Server components (the default) ship zero JavaScript and can fetch data directly. Only add 'use client' to components that need hooks, event handlers, or browser APIs. Keeping most components as server components reduces bundle size.
The mistake
Using useEffect for data fetching in Next.js server components
The fix
Server components can fetch data directly with async/await — no useEffect needed. useEffect is for client-side side effects. Fetching in useEffect causes a loading flash and waterfalls requests.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.