If React is the king of frontend UI, then Next.js is the “magic tool” that takes React to full-stack. It allows React to do more than just SPA (Single Page Applications)—it enables SSR (Server-Side Rendering), APIs, Edge Functions, and more.
What is Next.js?#
Next.js is a React full-stack framework built by Vercel (formerly Zeit), released in 2016. It builds a layer of abstraction on top of React, providing server-side rendering, static generation, and API routing capabilities.
Understanding Through Analogy#
| Aspect | Plain React | Next.js |
|---|---|---|
| Page rendering | Client-side rendering (CSR) | Supports SSR, SSG, ISR |
| Routing | Need React Router | File-system routing (automatic) |
| API | Need to build separately | Built-in API Routes |
| SEO | Poor (needs extra work) | Naturally SEO-friendly |
| Like | A car with only an engine | A complete car |
Core Features#
1. Multiple Rendering Modes: Choose as Needed#
Next.js supports various rendering strategies, allowing you to choose the most appropriate one for each scenario:
Client-Side Rendering (CSR)#
Traditional React rendering approach, pages generated in the browser.
// app/page.js (App Router)
'use client';
import { useState, useEffect } from 'react';
export default function Page() {
const [data, setData] = useState(null);
useEffect(() => {
fetch('/api/data').then(res => res.json()).then(setData);
}, []);
return <div>{data ? data.message : 'Loading...'}</div>;
}Server-Side Rendering (SSR)#
Pages are rendered on the server, regenerated on each request.
// app/page.js
async function getData() {
const res = await fetch('https://api.example.com/data', {
cache: 'no-store' // Re-fetch on every request
});
return res.json();
}
export default async function Page() {
const data = await getData();
return <div>{data.message}</div>;
}Static Site Generation (SSG)#
Pages are generated at build time, built once, cached forever.
// app/blog/[slug]/page.js
async function getPost(slug) {
const res = await fetch(`https://api.example.com/posts/${slug}`);
return res.json();
}
export async function generateStaticParams() {
const posts = await fetch('https://api.example.com/posts').then(r => r.json());
return posts.map((post) => ({ slug: post.slug }));
}
export default async function PostPage({ params }) {
const post = await getPost(params.slug);
return <article>{post.content}</article>;
}Incremental Static Regeneration (ISR)#
Pages are statically generated but can be updated periodically.
// Revalidate every 60 seconds
export const revalidate = 60;
async function getData() {
const res = await fetch('https://api.example.com/data');
return res.json();
}
export default async function Page() {
const data = await getData();
return <div>{data.content}</div>;
}2. App Router: The New Routing System#
Next.js 13+ introduced the App Router, a completely new routing and rendering paradigm:
app/
├── page.js // Home page (/)
├── layout.js // Root layout
├── blog/
│ ├── page.js // /blog
│ └── [slug]/
│ └── page.js // /blog/:slug
└── api/
└── route.js // /api/* API routesRole of layout.js:
// app/layout.js
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>
<nav>Navigation</nav>
<main>{children}</main>
<footer>Footer</footer>
</body>
</html>
);
}Layouts automatically wrap child pages and don’t re-render during page transitions—this is “persistent layouts.”
3. Server Components: Server-Side Components#
A major innovation of App Router is the default use of Server Components:
// This is a Server Component (default)
async function BlogPost({ id }) {
// Directly access data on the server
const post = await db.posts.find(id);
return <article>{post.content}</article>;
}Benefits of Server Components:
- Reduce client-side JavaScript bundle size
- Directly access backend resources (databases, file systems)
- Automatic code splitting
- Better SEO
4. API Routes: Built-in Backend#
Next.js allows you to write APIs directly in your project:
// app/api/user/route.js
import { NextResponse } from 'next/server';
export async function GET() {
const users = await db.users.findMany();
return NextResponse.json(users);
}
export async function POST(request) {
const body = await request.json();
const user = await db.users.create(body);
return NextResponse.json(user, { status: 201 });
}5. Image and Font Optimization#
Next.js has built-in optimization features:
import Image from 'next/image';
import { Inter } from 'next/font/google';
// Optimized font
const inter = Inter({ subsets: ['latin'] });
export default function Page() {
return (
<main className={inter.className}>
{/* Optimized image - auto format conversion, lazy loading */}
<Image
src="/hero.jpg"
alt="Hero"
width={800}
height={600}
priority // Preload critical above-the-fold images
/>
</main>
);
}Why is Next.js So Popular?#
1. Full-Stack Capabilities#
Next.js enables React developers to:
- Frontend: Build UI components
- Backend: Write API Routes
- Database: Access directly (via Server Components)
- Deployment: One-click deploy to Vercel
2. Performance Optimization#
Next.js does a lot of performance optimization:
- Automatic code splitting
- Image optimization
- Font optimization
- Prefetching
- Caching strategies
3. SEO-Friendly#
Server-side rendering and static generation make SEO simple:
- Search engines can crawl complete HTML directly
- Meta tags are easy to configure
- Open Graph images are auto-generated
4. Rich Ecosystem#
- Officially maintained by Vercel
- Synchronized updates with React
- Many community plugins
- Complete documentation and examples
Use Cases#
| Scenario | Suitability | Notes |
|---|---|---|
| Marketing websites | ⭐⭐⭐⭐⭐ | SSG + ISR, excellent performance |
| Blogs / Content sites | ⭐⭐⭐⭐⭐ | SSG, SEO-friendly |
| SaaS products | ⭐⭐⭐⭐⭐ | SSR + CSR hybrid |
| E-commerce sites | ⭐⭐⭐⭐⭐ | ISR, balance of performance and updates |
| Corporate websites | ⭐⭐⭐⭐⭐ | Static generation, fast loading |
| API backend | ⭐⭐⭐⭐ | API Routes are lightweight enough |
Learning Path#
Beginner (1-2 weeks)#
- Understand Next.js project structure
- Master App Router basics
- Learn pages and layouts
- Understand the difference between SSR, SSG, and CSR
Intermediate (2-3 weeks)#
- Deep dive into Server Components
- Master data fetching (fetch, Server Actions)
- Learn API Routes
- Understand route groups and middleware
Advanced (continuous learning)#
- Performance optimization techniques
- Vercel deployment
- Edge Functions
- Deep dive into caching mechanisms
Next.js 14/15 New Features#
| Feature | Description |
|---|---|
| Server Actions | Execute functions on the server, call like regular functions |
| Partial Prerendering | Hybrid rendering approach for static and dynamic |
| Turbopack | Rust-based bundler, 10x faster than Webpack |
| Serial streaming | More granular streaming rendering control |
Comparison with Other Frameworks#
| Feature | Next.js | Nuxt (Vue) | Remix |
|---|---|---|---|
| Framework base | React | Vue | React |
| Rendering modes | SSR/SSG/CSR | SSR/SSG/CSR | SSR (default) |
| Routing | File-system | File-system | File-system |
| API | Built-in | Built-in | Nested routes |
| Deployment | Vercel (native) | Any platform | Any platform |
| Ease of start | ⭐⭐⭐ | ⭐⭐⭐ | ⭐⭐⭐⭐ |
Simply put:
- Next.js: Best full-stack solution in the React ecosystem, backed by Vercel
- Nuxt: Full-stack choice for Vue developers
- Remix: More modern web standards理念, slightly steeper learning curve
Summary#
Next.js extends React from a frontend library to a complete full-stack framework:
- Multiple rendering modes—SSR, SSG, ISR, choose as needed
- App Router—Modern routing and layout system
- Server Components—Reduce client code, improve performance
- API Routes—Frontend and backend integrated development
- Vercel ecosystem—One-click deployment, ultimate experience
If you use React, Next.js is almost essential to learn. It not only improves development experience but also brings better performance and user experience.
Next up: Next.js takes the “React full-stack” route, while Astro takes a completely different approach—“static first”—achieving zero JavaScript output with its “Islands architecture.” Let’s dive into this “performance king” in the next article.
