Loading content...
Loading content...
Next.js is an open-source React framework used to build full-stack web applications. It is created and maintained by Vercel.
It allows you to:
Build UI using React components
Add backend logic using API routes
Improve performance with server-side rendering
It is built on Server Components, which help render content faster and more efficiently.
Before learning Next.js, you should know:
HTML
CSS
JavaScript
React
Install Node.js : Make sure Node.js is installed on your system.
Create a new Next.js app and run it locally
plaintext
npx create-next-app my-next-app
cd my-next-app
npm run devYou will see prompts like:
Project name
TypeScript (Yes/No)
ESLint (Yes/No)
Tailwind CSS (Yes/No)
App Router (Recommended: Yes)
javascript
npm create next-appYou will see options like:
TypeScript (Yes/No)
Tailwind CSS
App Router
ESLint
Choose recommended defaults for best setup
javascript
npm i next react react-dompackage.json:javascript
{
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
}
}javascript
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"export": "next export",
"lint": "next lint"
}dev → Start development server
build → Create production build
start → Run production server
lint → Check code quality
export → Convert to static site
javascript
npm install --save-dev typescript @types/react @types/nodeThen:
Rename .js → .ts or .tsx
Next.js auto-detects TypeScript
javascript
// pages/index.js
export default function Home() {
return (
<div>
<h1>Hello, World!</h1>
</div>
);
}pages/index.js → Home route (/)
Each file = one route
Component returns UI
Next.js uses folder structure for routing:
pages/index.js → /
pages/about.js → /about
Each page is a UI for a route
Shared UI across multiple pages
Do not re-render on navigation
plaintext
import Link from "next/link";
<Link href="/about">
About Page
</Link>Used for programmatic navigation
Works only in client components
Folder inside ( )
Helps organize routes without changing URL
Use [id] format
Example: /blog/[id]
loading.js → Show loading UI
error.js → Handle errors
route.js → Custom API handlers
Next.js improves SEO using:
Server-side rendering
Automatic code splitting
Meta tags support
Next.js allows backend logic inside the same project.
Example:
pages/api/users.js
Accessible via /api/users
Server-side using fetch
Server-side using libraries
Client-side using route handlers
Client-side using libraries (SWR, React Query)
Runs at build time
Best for static data
Runs on every request
Best for dynamic data
plaintext
export async function getStaticProps() {
const res = await fetch("https://jsonplaceholder.typicode.com/posts");
const data = await res.json();
return {
props: {
posts: data,
},
};
}Stores data to avoid repeated fetching
Updates cached data
Types:
Time-based
On-demand
Server-Side Rendering (SSR)
Static Site Generation (SSG)
File-Based Routing
API Routes
Code Splitting
Image Optimization
TypeScript Support
No built-in state management
No fixed styling solution
You can use:
Redux / Context API
Tailwind / CSS Modules
App Router is a modern routing system in Next.js.
Uses folder-based structure
Supports layouts and nested routes
Improves performance
More flexible than old pages router