Next.js Introduction
Next.js is a full-stack React framework for building fast, SEO-friendly web applications. It combines file-based routing, server rendering, static generation, API handling, and built-in performance optimizations (such as smarter image loading and code splitting) so you can ship production-ready apps with less setup.
What is Next.js
Next.js is an open-source framework built on top of React. It helps you build full-stack web applications by providing conventions and tooling for:
- Routing with the
app/orpages/directory - Server-side rendering (SSR)
- Static site generation (SSG)
- API endpoints
- Image and font optimization
- Built-in support for TypeScript, ESLint, and modern bundling
If React gives you UI building blocks, Next.js gives you a complete framework to ship applications faster.
Prerequisites
Before installing Next.js, make sure your environment is ready.
- Node.js version 14.0 or later (LTS recommended)
- VS Code or another code editor
Installation Guidelines
Create a new Next.js project
The easiest way is to use create-next-app.
1 | npx create-next-app@latest my-next-app |
During setup, you may be asked to choose options such as:
- TypeScript
- ESLint
- Tailwind CSS
src/directory usage- App Router
You can select based on your project preference. A safe default for new projects is to enable TypeScript, ESLint, and App Router.
Then move into the project directory:
1 | cd my-next-app |
Start the development server
Run:
1 | npm run dev |
Open your browser at http://localhost:3000.
You should see the default Next.js welcome page.
Project scripts
Common npm scripts:
npm run dev— start development servernpm run build— build for productionnpm run start— run production build locallynpm run lint— run lint checks
Basic Project Structure
For App Router projects, common folders include:
app/— routes, layouts, and pagespublic/— static assetsnext.config.jsornext.config.ts— framework configpackage.json— dependencies and scripts
A very simple page example in app/page.tsx:
1 | export default function HomePage() { |
Key Concepts
The Core Philosophy
In a standard React app (Vite/CRA), you ship an empty HTML file and a giant JavaScript bundle. The browser downloads the JS, runs it, and then the user sees the UI.
In Next.js, the server pre-renders the HTML. The user sees the content instantly, and then React “hydrates” it to make it interactive. This is why Next.js is the king of SEO and performance.
Routing: No More react-router-dom
Next.js uses File-Based Routing. You don’t write a <Routes> component. Instead, your file structure is your URL structure.
- App Router (Modern Standard): Uses the
app/directory. app/page.tsx→/app/about/page.tsx→/aboutapp/blog/[slug]/page.tsx→/blog/:slugapp/products/[id]/configs/[configId]/page.tsx→/products/:id/configs/:configId
The “Two Worlds”: Server vs. Client
This is the biggest shift for React devs. By default, every component in the app/ directory is a Server Component.
| Feature | Server Components (Default) | Client Components |
|---|---|---|
| Directives | None | Must add 'use client' at the top |
| State/Effects | No useState or useEffect |
All React hooks allowed |
| Data Fetching | Can be async (fetch directly) |
Use useEffect or SWR/TanStack |
| Bundle Size | 0kb sent to client | Sent to client |
Rule of Thumb: Use Server Components for fetching data and static UI. Use Client Components only when you need interactivity (onClick, forms, state).
Simplified Data Fetching
Forget useEffect for initial data. Since Server Components can be async, fetching data looks like standard JavaScript:
1 | // This runs on the server. No 'loading' state needed! |
Built-in Optimizations
Next.js replaces standard HTML tags with smarter versions:
<Link>: Automatically prefetches the linked page in the background.<Image>: Automatically resizes images, prevents layout shift, and serves WebP format.<Font>: Self-hosts Google Fonts so there’s zero layout shift on load.