Skip to main content

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

The pages 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.
pages/blog/[slug].js

Implementation details

1

File system scanning

During build time, Next.js scans the pages directory and automatically generates route manifests based on file names and directory structure.
2

Route matching

Incoming requests are matched against the route manifest using a priority system:
  1. Exact static paths (/about)
  2. Dynamic routes (/blog/[slug])
  3. Catch-all routes (/products/[...categories])
3

Component loading

Matched routes load their corresponding page component. Next.js automatically code-splits each route, loading only the JavaScript needed for that page.
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

The getStaticProps function runs at build time to fetch data and pass it as props to your page component.

Build-time execution

Understanding when and where getStaticProps executes is crucial for proper implementation.
getStaticProps runs in a Node.js environment during the build process, never in the browser.
1

Data fetching

Next.js executes getStaticProps for each static page during next build.
2

Page rendering

The returned props are passed to the page component, which renders to static HTML.
3

HTML generation

The rendered HTML and JSON data are written to disk in the .next directory.
4

Deployment

Static HTML files are deployed to CDN, ready to serve instantly on request.
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 a revalidate 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.
1

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.
2

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
3

Cache update

Once regeneration completes, Next.js replaces the cached page with the new version. The original user still received the stale page instantly.
4

Subsequent requests

Future users receive the freshly regenerated page until the next revalidation window.
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 in pages/api become API endpoints instead of pages.

Middleware and helpers

Implement reusable logic for authentication, validation, and 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 the pages directory becomes its own JavaScript bundle.

Dynamic imports

Manually code-split components that aren’t needed immediately.

Shared dependencies

Next.js automatically creates shared chunks for dependencies used across multiple pages.
1

Dependency analysis

During build, Next.js analyzes which dependencies are imported by each page.
2

Chunk generation

Dependencies used by multiple pages are extracted into shared chunks to avoid duplication.
3

Optimal loading

When navigating between pages, only new chunks are downloaded. Shared dependencies are cached.
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

Next.js removes unused exports from your dependencies, reducing bundle size.
All JavaScript is minified in production builds using Terser, reducing file size by 40-60%.
Static files are automatically compressed using gzip and brotli for faster transfer.
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.