Next.js
- Pronunciation
- NEKST jay-ES
In short
Next.js is a React framework that adds routing, server-side rendering, static generation and server code, so a whole web app can be built as one project.
What is Next.js?
Next.js was released in 2016 by the company now called Vercel, to fill the gaps React leaves open. React renders components, but a real site also needs pages and URLs, data loading, server rendering for speed and search engines, and a build setup. Next.js provides all of these with sensible defaults, so a new project starts with one command.
Routing is based on files: in the App Router, a folder such as app/blog/[slug] with a page.tsx file becomes the page at /blog/some-post. Components are React Server Components by default, which run on the server, can read a database directly and send only HTML to the browser; components that need interactivity are marked with "use client".
Each page can be rendered in the way that suits it: generated once at build time (SSG), rendered on every request (SSR), or regenerated in the background after a set time. Route handlers and server actions let the same project serve APIs and handle form submissions, and the framework also optimizes images, fonts and scripts.
A common misconception is that Next.js only runs on Vercel. It is open source and runs on any Node.js server, in a Docker container or as a static export for many sites, although some features fit Vercel's platform most naturally. It is not a replacement for React either; it is built on top of it.
Key takeaways
- Next.js is a React framework for full web applications.
- Folders and files in the app directory define the routes.
- Server Components run on the server; client components add interactivity.
- Pages can be static (SSG), rendered per request (SSR) or regenerated over time.
- It is open source and runs on any Node.js host, not only on Vercel.
Example
// A Server Component: runs on the server, sends only HTML
export default async function PostsPage() {
const res = await fetch("https://api.example.com/posts");
const posts: { id: number; title: string }[] = await res.json();
return (
<ul>
{posts.map((post) => (
<li key={post.id}>{post.title}</li>
))}
</ul>
);
}Readers ask
What is the difference between React and Next.js?
React is a library for building user interfaces from components. Next.js is a framework built on React that adds routing, server rendering, static generation, data loading and a build setup.
Is Next.js frontend or backend?
Both. It renders the user interface, but its Server Components, route handlers and server actions run on the server, so one Next.js project can contain the frontend and the backend logic.
Do you need Vercel to use Next.js?
No. Next.js is open source and can run on any server that runs Node.js, in Docker, or as a static export. Vercel, the company behind it, offers hosting that supports its features out of the box.
Often compared
See also
- ReactWeb Development, p. 36React is an open-source JavaScript library for building user interfaces from reusable components that update automatically when their data changes.
- SSRWeb Development, p. 43SSR is a technique where the server builds the full HTML for a page on each request, so users and search engines receive ready-to-read content immediately.
- SSGWeb Development, p. 42SSG is a technique that renders a website's pages to plain HTML files at build time, so servers or CDNs can deliver them instantly without extra work.
- HydrationWeb Development, p. 22Hydration is the step where JavaScript in the browser takes over server-rendered HTML, attaching event handlers and state so the page becomes interactive.
- FrameworkProgramming Fundamentals, p. 20A framework is a reusable foundation of code, tools, and conventions that provides the structure of an application, so developers only fill in their own logic.
- CSRWeb Development, p. 9CSR is a rendering approach where the browser downloads a mostly empty HTML page plus JavaScript, then builds the page content on the user's device.
Spotted a mistake or something missing on this page?Suggest an edit