Ali Hesari

Learn Next.js

Next.js is the React framework for production. Master server-side rendering, static site generation, and full-stack development to build fast, SEO-friendly web applications.

What is Next.js?

Next.js is a React framework that provides production-ready features out of the box. It extends React with server-side rendering, static site generation, API routes, and automatic code splitting for optimal performance.

Created by Vercel (formerly Zeit), Next.js has become the go-to framework for building production React applications. It's used by companies like Netflix, TikTok, Hulu, and many others for their web applications.

Key Features

Server-Side Rendering (SSR)

Render pages on the server for better SEO and performance.

Example: Blog posts, e-commerce product pages, dynamic content

Static Site Generation (SSG)

Pre-render pages at build time for maximum performance.

Example: Marketing sites, documentation, blog archives

API Routes

Build backend APIs directly in your Next.js application.

Example: REST endpoints, database connections, authentication

File-based Routing

Automatic routing based on your file structure.

Example: pages/about.js → /about, pages/blog/[slug].js → /blog/:slug

Image Optimization

Automatic image optimization and lazy loading.

Example: WebP conversion, responsive images, blur placeholders

Built-in CSS Support

Support for CSS modules, Sass, and styled-jsx.

Example: Component-scoped styles, global styles, CSS-in-JS

Common Use Cases

E-commerce Websites

Build fast, SEO-friendly online stores with dynamic product pages.

Examples:

  • Product catalogs
  • Shopping carts
  • Payment integration
  • Inventory management

Marketing Websites

Create high-performance marketing sites with excellent SEO.

Examples:

  • Landing pages
  • Corporate websites
  • Portfolio sites
  • Blog platforms

Full-Stack Applications

Build complete applications with frontend and backend in one framework.

Examples:

  • SaaS applications
  • Admin dashboards
  • User management systems
  • API services

Content Management Systems

Build headless CMS solutions with dynamic content delivery.

Examples:

  • Blog platforms
  • News sites
  • Documentation sites
  • Multi-tenant apps

Example Next.js App

Blog with API Routes

A blog application demonstrating SSR, API routes, and dynamic routing.

pages/index.js (Homepage with SSR)

import Link from 'next/link'

export default function Home({ posts }) {
  return (
    <div>
      <h1>My Blog</h1>
      <ul>
        {posts.map(post => (
          <li key={post.id}>
            <Link href={`/posts/${post.slug}`}>
              <a>{post.title}</a>
            </Link>
          </li>
        ))}
      </ul>
    </div>
  )
}

export async function getServerSideProps() {
  const res = await fetch('http://alihesari-admin:3000/api/posts')
  const posts = await res.json()
  
  return {
    props: { posts }
  }
}

pages/api/posts.js (API Route)

export default function handler(req, res) {
  if (req.method === 'GET') {
    const posts = [
      { id: 1, title: 'Hello World', slug: 'hello-world' },
      { id: 2, title: 'Next.js Tips', slug: 'nextjs-tips' }
    ]
    res.status(200).json(posts)
  } else {
    res.setHeader('Allow', ['GET'])
    res.status(405).end(`Method ${req.method} Not Allowed`)
  }
}

Next.js vs Alternatives

Next.js vs Create React App

Client-side only, simpler setup

Create React App Advantages:

  • Simpler setup
  • Faster development
  • Less complexity

Create React App Disadvantages:

  • No SSR/SSG
  • SEO limitations
  • Performance issues
  • No API routes

Next.js vs Gatsby

Static-first, GraphQL-focused

Gatsby Advantages:

  • Excellent performance
  • Great for static sites
  • Rich plugin ecosystem

Gatsby Disadvantages:

  • Learning curve
  • Less flexible for dynamic content
  • Build time issues

Next.js vs Nuxt.js

Vue.js equivalent, similar features

Nuxt.js Advantages:

  • Vue.js ecosystem
  • Similar DX
  • Good performance

Nuxt.js Disadvantages:

  • Smaller community
  • Less job opportunities
  • Fewer resources

Next.js Learning Path

1️⃣

Setup

Installation, project structure, routing

2️⃣

Rendering

SSR, SSG, ISR, data fetching

3️⃣

Full-Stack

API routes, database, authentication

4️⃣

Production

Deployment, optimization, monitoring

Subscribe toChangelog

📚
One useful email, most weeks.

Weekly notes on software engineering and building income on the side, plus what I am learning as I ship my own products.

By submitting this form, you'll be signed up to my free newsletter. I may also send you other emails about my courses. You can opt-out at any time. For more information, see our privacy policy.