📌 Advanced Routing and Navigation in Next.js #32
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.
-
📌 Advanced Routing and Navigation in Next.js
🚀 Day 1: Deep dive into dynamic routing, nested layouts, API routing, and middleware.
🟢 Section 1: Dynamic and Nested Routing in Next.js
🚀 1️⃣ Understanding Dynamic Routes in Next.js
Next.js makes file-system-based routing powerful by allowing dynamic paths. Think of it as a shopping mall:
/
) leads to multiple stores (pages)./store/[category]
).📌 How Dynamic Routes Work Under the Hood?
Unlike traditional React apps where routing is handled by
react-router-dom
, Next.js leverages the filesystem for routing. When a request comes in:/pages/
.[param].js
, Next.js extracts that value dynamically./user/[id]/orders/[orderId]
), Next.js recursively matches files.getStaticPaths
,getServerSideProps
) ensures dynamic content is resolved properly.🛠 Implementation: Basic Dynamic Route
📌 File structure:
📌 Code:
pages/users/[id].js
🔹 Behavior:
/users/101
→ RendersUser Profile: 101
./users/102
→ RendersUser Profile: 102
.⚡ How
[id].js
,[…slug].js
, and[[…slug]].js
Differ?📌 Example: Catch-All Dynamic Route (
pages/blog/[...slug].js
)🔹
/blog/2024/nextjs
→ RendersBlog Path: 2024/nextjs
🔹
/blog
→ RendersBlog Path: Home
📌 Data Fetching in Dynamic Routes:
getStaticPaths
vsgetServerSideProps
🚀 Why is
router.query.id
sometimesundefined
in SSR?useRouter()
hook runs only on the client-side.getServerSideProps()
to fetch data before rendering.✅ Fetching Data with
getServerSideProps()
🔹 Ensures
id
is available before render, avoiding undefined errors.✅ Pre-rendering Static Pages with
getStaticPaths()
🔹 Next.js pre-builds pages at build time (better for performance).
📌 Mermaid Diagram: How Dynamic Routing Works
🚀 Best Practices & Anti-Patterns in Dynamic Routing
✅ Things to Do (Best Practices)
✅ Use
getServerSideProps()
for dynamic data that changes frequently.✅ Use
getStaticPaths()
when the number of routes is known ahead of time for performance.✅ Always handle edge cases (e.g., user not found, API failure).
❌ Things to Avoid (Anti-Patterns)
❌ Do NOT use
useRouter().query.id
in SSR, as it will beundefined
.❌ Avoid deeply nested routes unless absolutely necessary for maintainability.
❌ Do NOT overuse
getServerSideProps()
if static generation is possible (bad for performance).🏗 Complete Node.js App: Dynamic User Profiles
📌 Project Structure:
📌 Code:
pages/users/[id].js
📌 Code:
pages/api/users.js
(Mock API for Users)📌 Code:
server.js
(Optional: Custom Express Server for Next.js)📌 Next Steps:
✅ Read Next.js Docs on Dynamic Routing.
✅ Implement your own nested dashboard with dynamic paths.
💡 Up Next: Section 2 - Nested Layouts and Shared UI 🔥
I'll now generate notes for Nested Layouts & Shared UI, followed by API Routes & Middleware. Let me know if you want to tweak anything. 🚀
I'll generate the GitHub Markdown notes step by step, starting with **Dynamic and Nested Routing**. Below is the first section, followed by more in subsequent responses.📌 Advanced Routing and Navigation in Next.js
🚀 Day 1: Deep dive into dynamic routing, nested layouts, API routing, and middleware.
🟢 Section 1: Dynamic and Nested Routing in Next.js
🚀 1️⃣ Understanding Dynamic Routes in Next.js
Next.js makes file-system-based routing powerful by allowing dynamic paths. Think of it as a shopping mall:
/
) leads to multiple stores (pages)./store/[category]
).📌 How Dynamic Routes Work Under the Hood?
Unlike traditional React apps where routing is handled by
react-router-dom
, Next.js leverages the filesystem for routing. When a request comes in:/pages/
.[param].js
, Next.js extracts that value dynamically./user/[id]/orders/[orderId]
), Next.js recursively matches files.getStaticPaths
,getServerSideProps
) ensures dynamic content is resolved properly.🛠 Implementation: Basic Dynamic Route
📌 File structure:
📌 Code:
pages/users/[id].js
🔹 Behavior:
/users/101
→ RendersUser Profile: 101
./users/102
→ RendersUser Profile: 102
.⚡ How
[id].js
,[…slug].js
, and[[…slug]].js
Differ?[id].js
/users/123
/users/:id
[...slug].js
/blog/2024/post
/blog/:slug*
(captures multiple segments)[[...slug]].js
/blog
,/blog/2024/post
📌 Example: Catch-All Dynamic Route (
pages/blog/[...slug].js
)🔹
/blog/2024/nextjs
→ RendersBlog Path: 2024/nextjs
🔹
/blog
→ RendersBlog Path: Home
📌 Data Fetching in Dynamic Routes:
getStaticPaths
vsgetServerSideProps
🚀 Why is
router.query.id
sometimesundefined
in SSR?useRouter()
hook runs only on the client-side.getServerSideProps()
to fetch data before rendering.✅ Fetching Data with
getServerSideProps()
🔹 Ensures
id
is available before render, avoiding undefined errors.✅ Pre-rendering Static Pages with
getStaticPaths()
🔹 Next.js pre-builds pages at build time (better for performance).
📌 Mermaid Diagram: How Dynamic Routing Works
🚀 Best Practices & Anti-Patterns in Dynamic Routing
✅ Things to Do (Best Practices)
✅ Use
getServerSideProps()
for dynamic data that changes frequently.✅ Use
getStaticPaths()
when the number of routes is known ahead of time for performance.✅ Always handle edge cases (e.g., user not found, API failure).
❌ Things to Avoid (Anti-Patterns)
❌ Do NOT use
useRouter().query.id
in SSR, as it will beundefined
.❌ Avoid deeply nested routes unless absolutely necessary for maintainability.
❌ Do NOT overuse
getServerSideProps()
if static generation is possible (bad for performance).🏗 Complete Node.js App: Dynamic User Profiles
📌 Project Structure:
📌 Code:
pages/users/[id].js
📌 Code:
pages/api/users.js
(Mock API for Users)📌 Code:
server.js
(Optional: Custom Express Server for Next.js)📌 Next Steps:
✅ Read [Next.js Docs on Dynamic Routing](https://nextjs.org/docs/routing/dynamic-routes).
✅ Implement your own nested dashboard with dynamic paths.
Beta Was this translation helpful? Give feedback.
All reactions