What is NextJS?
NextJS is a React framework for building full-stack web applications. It provides additional structure, features, and optimizations for your React applications, including:- File-based routing - No need to configure routes manually
- Server-side rendering (SSR) - Pre-render pages on the server
- Static site generation (SSG) - Generate static HTML at build time
- API routes - Build your backend API within the same project
- Automatic code splitting - Better performance out of the box
This course covers both the traditional Pages Router (section 25) and the modern App Router (section 25+), giving you a complete understanding of NextJS development.
Project Setup
Create a new NextJS project:Package Configuration
Here’s a typical NextJS project setup:package.json
dev
Runs development server with hot reloading
build
Creates optimized production build
start
Runs production server
export
Exports static HTML files
Pages Router Fundamentals
The traditional NextJS routing system uses apages/ directory where each file automatically becomes a route.
Creating Your First Page
pages/news.js
File names in the
pages/ directory directly correspond to URL paths. pages/news.js → /newsDynamic Routes
Create dynamic routes using square brackets[param] in file names:
pages/news/[newsId].js
Data Fetching Methods
NextJS provides several methods for fetching data at different stages of the rendering process.Static Site Generation (SSG)
UsegetStaticProps to fetch data at build time:
pages/index.js
When to use getStaticProps
When to use getStaticProps
- Data doesn’t change frequently
- Content can be pre-rendered at build time
- SEO is important
- Page must be fast (pre-rendered HTML)
Incremental Static Regeneration (ISR)
Addrevalidate to regenerate pages in the background:
pages/index.js
Server-Side Rendering (SSR)
UsegetServerSideProps to fetch data on every request:
pages/index.js
SSR vs SSG
SSR: Data fetched on every request - always fresh but slowerSSG: Data fetched at build time - faster but may be stale
The _app.js File
The_app.js file is a wrapper around all your pages:
pages/_app.js
API Routes
Create API endpoints inside thepages/api/ directory:
pages/api/hello.js
/api/hello.
Key Concepts Summary
Next Steps
App Router
Learn the modern App Router with enhanced features
Server Components
Understand React Server Components in NextJS