Next.js Fundamentals
Next.js is a React framework that enables server-side rendering, static site generation, and other advanced features with minimal configuration.
Introduction to Next.js
Next.js was created by Vercel and provides:
- Server-side rendering (SSR): Renders pages on the server for better SEO and initial load performance
- Static site generation (SSG): Pre-renders pages at build time for optimal performance
- Automatic code splitting: Only loads JavaScript needed for each page
- Built-in CSS/Sass support: Import CSS/Sass files directly in components
- API routes: Create API endpoints as Node.js serverless functions
- Developer experience: Hot reloading, error reporting, and more
Setting Up a Next.js Project
Creating a New Project
1 | npx create-next-app my-nextjs-app |
Project Structure
1 | my-nextjs-app/ |
Pages and Routing
Next.js has a file-system based router built on the concept of pages:
Basic Pages
1 | // pages/index.js - Route: / |
Dynamic Routes
1 | // pages/posts/[id].js - Route: /posts/:id |
Nested Dynamic Routes
1 | // pages/[category]/[product].js - Route: /:category/:product |
Catch-All Routes
1 | // pages/blog/[...slug].js - Routes: /blog/2020/01/01, /blog/category/post |
Navigation
Link Component
1 | import Link from 'next/link'; |
Programmatic Navigation
1 | import { useRouter } from 'next/router'; |
Data Fetching
Next.js has built-in data fetching methods for different use cases:
getStaticProps (Static Site Generation)
Use for data available at build time:
1 | // pages/posts.js |
getStaticPaths (Dynamic Routes with SSG)
Use with dynamic routes and getStaticProps
:
1 | // pages/posts/[id].js |
getServerSideProps (Server-Side Rendering)
Use for data that must be fetched at request time:
1 | // pages/profile.js |
Client-Side Data Fetching
For data that doesn’t need SEO or can be loaded after the page loads:
1 | import { useState, useEffect } from 'react'; |
SWR for Data Fetching
Next.js recommends SWR for client-side data fetching:
1 | import useSWR from 'swr'; |
API Routes
Next.js allows you to create API endpoints within your application:
1 | // pages/api/hello.js |
Request Methods
1 | // pages/api/users.js |
Dynamic API Routes
1 | // pages/api/users/[id].js |
Styling in Next.js
Next.js supports various styling options:
Global CSS
1 | // pages/_app.js |
CSS Modules
1 | // styles/Home.module.css |
Sass/SCSS
1 | // Install Sass |
CSS-in-JS (Styled JSX)
1 | export default function Button() { |
Image Optimization
Next.js includes an Image component for automatic image optimization:
1 | import Image from 'next/image'; |
Head Management
Next.js has a built-in component for managing <head>
elements:
1 | import Head from 'next/head'; |
Custom Document and App
Custom Document
Customize the HTML document structure:
1 | // pages/_document.js |
Custom App
For persistent layouts, global state, or additional providers:
1 | // pages/_app.js |
Environment Variables
Next.js comes with built-in support for environment variables:
1 | # .env.local (not committed to git) |
1 | // Access in Node.js environment (SSR, API routes) |
Deployment
Static Export
For static sites that can be hosted anywhere:
1 | # Configure in next.config.js |
Vercel (Recommended)
Deploy directly from Git:
1 | # Install Vercel CLI |
Other Hosting Providers
For Node.js hosting:
1 | # Build the application |
Advanced Features
Middleware
Execute code before requests are completed:
1 | // middleware.ts |
Internationalized Routing
1 | // next.config.js |
Best Practices
- Use Static Generation when possible for better performance
- Incremental Static Regeneration for dynamic content that changes infrequently
- Server-Side Rendering for pages that need request-time data
- Client-side fetching for private, user-specific data
- Optimize images with the Next.js Image component
- Code splitting happens automatically per page
- Pre-fetch links automatically with the Link component
- API Routes for backend functionality instead of external APIs
- Environment Variables for configuration
- TypeScript support for better developer experience