Next.js

On this page 7

Next.js is an open-source framework built on React for creating web applications that render on the server, at build time or in the browser, chosen per route or even per component. Among web applications frameworks, it is a meta-framework: React supplies the component model, while Next.js adds file-based routing, rendering strategies, data fetching, bundling and deployment conventions.

The framework became popular because it solves problems that a plain single-page application leaves open, especially slow first loads and weak indexability of client-rendered pages. Its current architecture, the App Router, is built around React Server Components, while the older Pages Router remains supported for existing projects.

How Next.js renders a request

In the App Router, every component is a React Server Component by default. It runs only on the server, can read databases or files directly and sends a serialized result to the browser, so its code never enters the client bundle. Components marked with the "use client" directive are also rendered to HTML on the server but then hydrated in the browser to become interactive.

For each request, the framework resolves the route from the folder structure, renders the server tree, streams HTML in chunks as data becomes available and sends the payload that lets React attach event handlers. Suspense boundaries define which parts may appear later without holding up the rest of the page.

Building blockRoleWhere it runs
Server ComponentsFetch data and render markupServer or build machine
Client ComponentsHandle state, effects and eventsServer for HTML, then browser
Route handlersExpose HTTP endpointsServer or edge runtime
Server ActionsRun mutations called from formsServer
MiddlewareRewrite, redirect or check requests earlyEdge-style runtime
LayoutsShare UI that persists across navigationServer by default
Building blocks of a Next.js application

Rendering strategies and when each applies

The central idea of Next.js is that one application can mix rendering strategies. A marketing page can be static, a product page can be regenerated periodically and a dashboard can be rendered fresh for each user, all in the same codebase.

StrategyWhen HTML is producedBest fit
SSGAt build timeDocumentation, landing pages, blogs
ISRAt build, then regenerated after a set interval or on demandCatalogs and content that changes hourly
SSROn every requestPersonalized or real-time pages
Streaming SSRProgressively during the requestPages with slow independent data sources
Client-side renderingIn the browser after loadHighly interactive widgets
Partial prerenderingStatic shell plus dynamic holesMostly static pages with small personal parts
Rendering strategies available in Next.js

Caching rules decide which strategy a route actually uses. Reading request data such as cookies or headers makes a route dynamic, while a revalidation interval, for example 60 seconds, turns it into incremental static regeneration. Defaults have changed between major versions, so explicit cache settings are safer than relying on implicit behavior.

Comparison with neighboring frameworks

Next.js competes with other full-stack meta-frameworks and also with plain client-side setups. The comparison below focuses on architectural choices rather than popularity.

ApproachUI libraryDefault renderingTypical strength
Next.jsReactServer Components with streamingWide rendering choice, large ecosystem
React SPA with a bundlerReactClient-side renderingSimple hosting as static files
NuxtVue.jsUniversal SSRSimilar model for Vue teams
SvelteKitSvelteSSR with progressive enhancementSmall client bundles
AstroSeveralStatic HTML with islandsContent-heavy sites with little JavaScript
Next.js compared with related approaches

Choosing Next.js makes most sense when a team already uses React and needs both SEO-friendly pages and rich interactivity. For a mostly static content site, an islands architecture often ships far less JavaScript.

Routing, data and deployment in practice

Routes are defined by folders inside the app directory. A page file makes a segment publicly reachable, layout wraps child segments, loading provides a Suspense fallback and error defines an error boundary. Dynamic segments such as [slug] capture URL parameters.

  • Data fetching happens directly in async Server Components, without a separate API layer for internal data.
  • Image, font and script components handle resizing, font self-hosting and loading priority.
  • Deployment targets include managed platforms, a Node.js server, containers and static export with reduced features.
  • Edge runtime support allows some routes to run close to users, with a restricted set of Node.js APIs.

Self-hosting is fully possible, but features such as image optimization, ISR caches and middleware need careful configuration on infrastructure other than the platform maintained by the framework’s vendor.

Limitations and common pitfalls

The flexibility of Next.js brings a real learning curve. Most problems arise from misunderstanding where code runs and what is cached.

ProblemCauseRemedy
Hydration mismatch warningsServer and browser render different outputAvoid dates, random values or browser APIs during render
Stale data in productionRoute cached more than expectedSet revalidation or dynamic rendering explicitly
Large client bundles“use client” placed too high in the treePush client boundaries down to leaf components
Secrets exposedServer-only code imported into client modulesUse server-only modules and environment rules
Slow buildsThousands of statically generated pagesUse ISR or on-demand generation
Frequent problems and remedies

Frequent major releases also mean that tutorials age quickly. Teams should check which router and version an example targets before copying its patterns.

A plain-language summary

React is a toolkit for building the parts of a web page, like buttons, menus and product cards. Next.js is the workshop around that toolkit: it decides where each page is prepared, how pages link together and how they reach visitors quickly.

Some pages can be prepared in advance like printed brochures, while others are made fresh for each visitor like a custom order. Next.js lets one website use both methods at the same time, so pages load fast and still show up to date information.

An example from practice

A mid-sized online furniture retailer runs its storefront as a client-rendered React application. Product pages show an empty frame for over a second on phones, and search engines index the product descriptions inconsistently.

The team migrates to Next.js with the App Router. Category and product pages become statically generated with a 10-minute revalidation interval, the shopping cart and account area are rendered dynamically for each user, and the image gallery stays a small Client Component. Price updates from the inventory system trigger on-demand revalidation of the affected products.

After the migration, product pages arrive as complete HTML on the first request, the amount of JavaScript sent to phones falls noticeably, and search results begin to show accurate descriptions and prices.