What is Next.js?
Next.js is a React framework that provides everything you need to build production-ready web applications. While React is a library for building user interfaces, Next.js is a complete framework that handles routing, bundling, optimization, and deployment.
Key Features That Make Next.js Special
Multiple Rendering Methods:
- Static Site Generation (SSG) for lightning-fast sites
- Server-Side Rendering (SSR) for dynamic content
- Client-Side Rendering (CSR) when needed
- Incremental Static Regeneration (ISR) for the best of both worlds
Built-in Optimizations:
- Automatic code splitting
- Image optimization with next/image
- Font optimization
- Script optimization
- Bundle analyzer
Developer Experience:
- File-based routing (no need for routing libraries)
- API routes (build your backend in the same project)
- Built-in CSS and Sass support
- TypeScript support out of the box
- Hot reloading and fast refresh
Modern Features (Next.js 13+):
- App Router with nested layouts
- Server Components and Client Components
- Streaming and Suspense
- Middleware and Edge Runtime
Simple Example
Pages Router (Traditional):
// pages/index.js
export default function Home({ posts }) {
return (
<div>
<h1>My Blog</h1>
{posts.map(post => (
<article key={post.id}>
<h2>{post.title}</h2>
<p>{post.excerpt}</p>
</article>
))}
</div>
)
}
// This runs at build time
export async function getStaticProps() {
const posts = await fetch('https://api.example.com/posts').then(r => r.json())
return { props: { posts } }
}
App Router (Next.js 13+):
// app/page.js
export default async function Home() {
const posts = await fetch('https://api.example.com/posts', {
next: { revalidate: 3600 } // ISR: revalidate every hour
}).then(r => r.json())
return (
<div>
<h1>My Blog</h1>
{posts.map(post => (
<article key={post.id}>
<h2>{post.title}</h2>
<p>{post.excerpt}</p>
</article>
))}
</div>
)
}