Introduction to Next.js
What is Next.js?
-
Next.js is a React framework for building server-rendered and static web applications.
-
It simplifies building React apps with built-in routing, server-side rendering (SSR), static site generation (SSG), and API routes.
-
Created by Vercel and widely used for production React apps.
Why Use Next.js?
-
Automatic routing based on the
pagesdirectory. -
Supports Server-Side Rendering (SSR) and Static Site Generation (SSG).
-
Built-in CSS and Sass support.
-
API routes for backend functionality inside the same app.
-
Optimized for performance, SEO-friendly.
-
Easy deployment with Vercel or other platforms.
Basic Next.js App Structure
Creating Pages
Each file in the pages/ folder automatically becomes a route.
pages/index.js
pages/about.js
Visiting / shows Home page, /about shows About page.
Server-Side Rendering (SSR)
Next.js lets you fetch data on the server at request time using getServerSideProps.
Static Site Generation (SSG)
Generate pages at build time using getStaticProps.
API Routes
Define backend endpoints inside pages/api.
pages/api/hello.js
Running a Next.js App
Open http://localhost:3000 in your browser.
Summary
| Feature | Description |
|---|---|
| File-based Routing | Pages auto-routed based on pages folder |
| SSR & SSG | Render pages on server or at build time |
| API Routes | Serverless backend functions inside app |
| Built-in CSS Support | Supports CSS, Sass, and CSS modules |
| Easy Deployment | Optimized for deployment on Vercel and others |
