📌 Next.js Routing: Pages Router vs. App Router #28
akash-coded
started this conversation in
Notes
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Here are the detailed notes on Routing in Next.js, covering both Pages Router vs. App Router, along with static and dynamic routing, when to use what, best practices, pitfalls, and a complete hands-on example.
📌 Next.js Routing: Pages Router vs. App Router
🏗 1️⃣ Pages Router vs. App Router: Understanding the Evolution
🔷 What is the Pages Router?
The Pages Router is the traditional way of routing in Next.js (before Next.js 13). It uses a file-based routing system inside the
/pages
directory.✅ Key Features:
.js
or.tsx
file inside/pages
automatically becomes a route.getStaticProps
,getServerSideProps
,getStaticPaths
.📌 Example:
🔹 How to Fetch Data in Pages Router?
🔷 What is the App Router? (Next.js 13+)
The App Router (
/app
directory) introduces a React Server Component-first approach, making data fetching more efficient and scalable.✅ Key Features:
getServerSideProps
,getStaticProps
.📌 Example:
🔹 Fetching Data in App Router (Server Component)
🛠 2️⃣ Static vs. Dynamic Routing in Next.js
Both Pages Router and App Router support static and dynamic routing, but the implementation differs.
🔹 Static Routing: When the Route Structure is Fixed
✅ Use when pages are known beforehand.
✅ Faster because it can be pre-rendered at build time.
✅ Good for homepages, about pages, documentation, etc.
📌 Pages Router (Static Route Example)
📌 App Router (Static Route Example)
🔹 Dynamic Routing: When the Route is Unknown in Advance
✅ Use when page paths depend on dynamic data (e.g., users, products).
✅ Can be server-rendered (SSR) or statically generated (SSG).
✅ Good for user profiles, blog posts, e-commerce product pages.
📌 Pages Router (Dynamic Route Example)
📌 App Router (Dynamic Route Example)
🏗 3️⃣ When to Use What?
✅ Use Pages Router if:
✅ Use App Router if:
📌 4️⃣ Common Pitfalls & Best Practices
❌ Anti-Patterns in Next.js Routing
🚨 1. Avoid using both
/pages
and/app
together in one project.❌ Mixing the two can cause unexpected behavior in Next.js 13+.
🚨 2. Don't overuse
getServerSideProps()
if static generation is possible.❌ SSR slows down performance if you fetch data on every request.
🚨 3. Avoid using
useRouter().query
for SSR.❌ The
useRouter()
hook doesn't work on the server, leading to undefined errors.✅ Best Practices in Next.js Routing
✅ 1. Use Static Generation (
getStaticProps()
) whenever possible.✅ 2. Use App Router for modern projects needing streaming, layouts.
✅ 3. Organize route structures logically (avoid deep nesting).
🛠 5️⃣ Hands-on Project: Next.js Routing Example
📌 Project Structure
📌 Code:
app/products/[id]/page.tsx
📌 Code:
app/layout.tsx
(Global Layout)📌 Next Steps: ✅ Read Next.js Docs on Routing.
✅ Build a dynamic blog page using the App Router.
Let me know if you need any refinements! 🚀 Next, we can dive deeper into layouts or API routes.
Here are the **detailed GitHub Markdown notes** on **Routing in Next.js**, covering both **Pages Router vs. App Router**, along with **static and dynamic routing**, when to use what, best practices, pitfalls, and a complete hands-on example.📌 Next.js Routing: Pages Router vs. App Router
🏗 1️⃣ Pages Router vs. App Router: Understanding the Evolution
🔷 What is the Pages Router?
The Pages Router is the traditional way of routing in Next.js (before Next.js 13). It uses a file-based routing system inside the
/pages
directory.✅ Key Features:
.js
or.tsx
file inside/pages
automatically becomes a route.getStaticProps
,getServerSideProps
,getStaticPaths
.📌 Example:
🔹 How to Fetch Data in Pages Router?
🔷 What is the App Router? (Next.js 13+)
The App Router (
/app
directory) introduces a React Server Component-first approach, making data fetching more efficient and scalable.✅ Key Features:
getServerSideProps
,getStaticProps
.📌 Example:
🔹 Fetching Data in App Router (Server Component)
🛠 2️⃣ Static vs. Dynamic Routing in Next.js
Both Pages Router and App Router support static and dynamic routing, but the implementation differs.
🔹 Static Routing: When the Route Structure is Fixed
✅ Use when pages are known beforehand.
✅ Faster because it can be pre-rendered at build time.
✅ Good for homepages, about pages, documentation, etc.
📌 Pages Router (Static Route Example)
/pages ├── about.js # → `/about`
📌 App Router (Static Route Example)
/app ├── about │ ├── page.tsx # → `/about`
🔹 Dynamic Routing: When the Route is Unknown in Advance
✅ Use when page paths depend on dynamic data (e.g., users, products).
✅ Can be server-rendered (SSR) or statically generated (SSG).
✅ Good for user profiles, blog posts, e-commerce product pages.
📌 Pages Router (Dynamic Route Example)
/pages ├── products │ ├── [id].js # → `/products/:id`
📌 App Router (Dynamic Route Example)
/app ├── products │ ├── [id] │ │ ├── page.tsx # → `/products/:id`
🏗 3️⃣ When to Use What?
/pages
)/app
)/pages
folder/app
foldergetStaticProps
,getServerSideProps
fetch()
directly in Server ComponentsgetStaticProps
head
metadata✅ Use Pages Router if:
✅ Use App Router if:
📌 4️⃣ Common Pitfalls & Best Practices
❌ Anti-Patterns in Next.js Routing
🚨 1. Avoid using both
/pages
and/app
together in one project.❌ Mixing the two can cause unexpected behavior in Next.js 13+.
🚨 2. Don't overuse
getServerSideProps()
if static generation is possible.❌ SSR slows down performance if you fetch data on every request.
🚨 3. Avoid using
useRouter().query
for SSR.❌ The
useRouter()
hook doesn't work on the server, leading to undefined errors.✅ Best Practices in Next.js Routing
✅ 1. Use Static Generation (
getStaticProps()
) whenever possible.✅ 2. Use App Router for modern projects needing streaming, layouts.
✅ 3. Organize route structures logically (avoid deep nesting).
🛠 5️⃣ Hands-on Project: Next.js Routing Example
📌 Project Structure
📌 Code:
app/products/[id]/page.tsx
📌 Code:
app/layout.tsx
(Global Layout)📌 Next Steps:
✅ Read [Next.js Docs on Routing](https://nextjs.org/docs/app/building-your-application/routing).
✅ Build a dynamic blog page using the App Router.
Beta Was this translation helpful? Give feedback.
All reactions