Overview
Next.js is a React framework that provides a complete architecture for building production-ready applications. It combines server-side rendering, static generation, API routes, and optimized bundling into a cohesive system built on React.Next.js abstracts complex infrastructure decisions while remaining flexible enough to optimize for different use cases through its hybrid rendering model.
File-based routing
Next.js implements routing through the file system structure, eliminating the need for explicit route configuration.How it works
Thepages directory (or app directory in Next.js 13+) maps directly to routes in your application.
Dynamic routes
Files with bracket notation create dynamic route segments.- Single parameter
- Multiple parameters
- Catch-all routes
pages/blog/[slug].js
Implementation details
File system scanning
During build time, Next.js scans the
pages directory and automatically generates route manifests based on file names and directory structure.Route matching
Incoming requests are matched against the route manifest using a priority system:
- Exact static paths (
/about) - Dynamic routes (
/blog/[slug]) - Catch-all routes (
/products/[...categories])
Performance benefit: File-based routing enables automatic code splitting, ensuring users only download the code for routes they visit.
Static Site Generation (SSG)
SSG pre-renders pages at build time, generating static HTML that can be served instantly from a CDN.getStaticProps
ThegetStaticProps function runs at build time to fetch data and pass it as props to your page component.
Build-time execution
Understanding when and wheregetStaticProps executes is crucial for proper implementation.
Execution environment
Execution environment
getStaticProps runs in a Node.js environment during the build process, never in the browser.Build process
Build process
Dynamic routes with SSG
Dynamic routes with SSG
For dynamic routes, combine
getStaticProps with getStaticPaths to specify which paths to pre-render.pages/blog/[slug].js
When to use SSG
Marketing pages
Landing pages, about pages, and marketing content that rarely changes benefit from instant CDN delivery.
Blog posts
Content-focused sites where posts are written once and served many times.
Documentation
Technical documentation that updates infrequently but needs to be fast and SEO-friendly.
E-commerce listings
Product pages with relatively stable content (combine with ISR for updates).
Incremental Static Regeneration (ISR)
ISR enables updating static pages after build time without rebuilding the entire site, combining the performance of SSG with the freshness of SSR.Revalidation mechanism
Add arevalidate property to getStaticProps to specify how often Next.js should regenerate the page.
Stale-while-revalidate strategy
ISR implements a stale-while-revalidate caching pattern that balances freshness and performance.First request (cache hit)
User requests a page. Next.js serves the cached static HTML instantly from CDN. The page may be stale, but the user sees content immediately.
Background regeneration
If the page is older than the revalidation time, Next.js triggers a background regeneration.
- Fetches fresh data by executing
getStaticProps - Renders new HTML with updated data
- Does NOT block the current request
Cache update
Once regeneration completes, Next.js replaces the cached page with the new version. The original user still received the stale page instantly.
- Timeline example
- Revalidation flow
Key insight: ISR guarantees users always get fast responses (serving stale content) while ensuring content stays reasonably fresh through background updates.
On-demand revalidation
Trigger revalidation programmatically when you know content has changed, without waiting for the time-based window.API routes
API routes provide a serverless backend solution within your Next.js application, running as serverless functions.Creating API endpoints
Files inpages/api become API endpoints instead of pages.
Middleware and helpers
Implement reusable logic for authentication, validation, and error handling.- Authentication
- Error handling
lib/auth.js
pages/api/profile.js
Use cases
Form submissions
Handle contact forms, newsletter signups, and user-generated content.
Webhooks
Receive and process webhooks from third-party services like Stripe or GitHub.
Proxy endpoints
Hide API keys by proxying requests to external services through your backend.
Data aggregation
Combine data from multiple sources before sending to the client.
Module bundling and code splitting
Next.js automatically optimizes your JavaScript bundles for optimal loading performance.Automatic code splitting
Every page in thepages directory becomes its own JavaScript bundle.
Dynamic imports
Manually code-split components that aren’t needed immediately.- Client-side
- Named exports
Shared dependencies
Next.js automatically creates shared chunks for dependencies used across multiple pages.Chunk generation
Dependencies used by multiple pages are extracted into shared chunks to avoid duplication.
Next.js uses webpack (or Turbopack in Next.js 13+) with carefully tuned configuration to balance chunk size, caching efficiency, and loading performance.
Bundle optimization techniques
Tree shaking
Tree shaking
Next.js removes unused exports from your dependencies, reducing bundle size.
Minification
Minification
All JavaScript is minified in production builds using Terser, reducing file size by 40-60%.
Compression
Compression
Static files are automatically compressed using gzip and brotli for faster transfer.
Image optimization
Image optimization
The
next/image component automatically optimizes images, serving modern formats (WebP, AVIF) and appropriate sizes.Architecture summary
Next.js combines multiple rendering strategies and architectural patterns into a cohesive framework:File-based routing
Zero-config routing with automatic code splitting per page.
SSG with getStaticProps
Build-time rendering for maximum performance and SEO.
ISR
Static generation with background updates for fresh content.
API routes
Serverless backend functions co-located with frontend code.
Automatic bundling
Optimized code splitting and chunk generation.
Hybrid rendering
Mix SSG, ISR, and SSR on a per-page basis.