๐ Next.js 16: Dynamic Routes, CatchโAll & SEOโFriendly URLs
Static routes like /blog or /about are easy to manage, but realโworld applications (Eโcommerce, Social Media, Blogs) handle thousands of unique pages for products, users, and posts. Creating manual folders for each is impossible โ this is where Dynamic Routing comes in.
๐ง 1. The Need for Dynamic Routes
Imagine a blog with 500 posts. Instead of creating 500 folders, we create one dynamic folder that acts as a template for all posts.
- Static Route:
/blog/contact(Fixed) - Dynamic Route:
/blog/[slug](Variable)
๐งฉ The [slug] vs [id] Concept
-
[slug]โ Use for userโfacing URLs (SEO friendly)- Example:
/blog/how-to-learn-nextjs
- Example:
-
[id]โ Use for internal systems or dashboards- Example:
/admin/orders/12345
- Example:
๐ฅ 2. Next.js 16 Reality: Asynchronous Params
In Next.js 16, a major shift happened: params and searchParams are now Promises.
โ Mandatory Rule
You cannot destructure params directly in the function argument anymore. You must await them inside the component because Server Components are async by default.
โ Correct Implementation (Server Component)
// app/blog/[slug]/page.jsx
export default async function BlogPostPage({ params, searchParams }) {
// Both are Promises in Next.js 16
const { slug } = await params;
const { ref, lang } = await searchParams;
return (
<main>
<h1>Post: {slug}</h1>
<div className="metadata">
<p>Source: {ref}</p>
<p>Language: {lang}</p>
</div>
</main>
);
}URL Example
/blog/nextjs-anatomy?ref=youtube&lang=en
slugโnextjs-anatomyrefโyoutube
๐ 3. params vs searchParams
Understanding the difference is crucial for SEO and application state.
| Feature | params |
searchParams |
|---|---|---|
| Location | Part of the URL path (/blog/post-1) |
Query string (?page=2) |
| Linkage | Directly linked to folder structure | Not linked to folders |
| SEO Impact | Very High (Google crawls paths) | Low (Filters / tracking) |
| Optionality | Mandatory for the route | Optional |
๐งฉ The House Analogy
paramsโ Your house address (City / Street / House No.) โ This is how people and Google find you.searchParamsโ A note on the doorbell ("Ring twice") โ Extra instructions, not location.
๐ ๏ธ 4. DataโDriven Routing & SEO 404s
When fetching data dynamically, you must handle cases where a slug does not exist in the database.
import { notFound } from "next/navigation";
const db = {
"nextjs-intro": "Welcome to Next.js",
"react-hooks": "Mastering Hooks"
};
export default async function Page({ params }) {
const { slug } = await params;
const content = db[slug];
if (!content) {
// Triggers not-found.jsx and sends proper 404 status to Google
notFound();
}
return <h1>{content}</h1>;
}๐ 5. Advanced Routing: CatchโAll Segments
Kabhiโkabhi URL ki depth unknown hoti hai (Docs site, file explorer, category trees).
A. CatchโAll [...slug]
- Handles:
/docs/react,/docs/react/hooks - Output:
['react', 'hooks']
โ ๏ธ Limitation: Root /docs match nahi karega โ 404.
B. Optional CatchโAll [[...slug]]
Double brackets segment ko optional bana dete hain.
| URL | slug value |
|---|---|
/docs |
undefined |
/docs/react |
['react'] |
/docs/react/hooks |
['react','hooks'] |
๐ฑ๏ธ 6. Routing in Client Components
Client Components route data ko hooks ke through access karte hain.
"use client";
import { useParams, useSearchParams } from "next/navigation";
export default function ClientComponent() {
const params = useParams();
const searchParams = useSearchParams();
return (
<div>
<p>Path Slug: {params.slug}</p>
<p>Query Ref: {searchParams.get("ref")}</p>
</div>
);
}๐ 7. SEOโFriendly URL Rules
- Use meaningful words โ
/blog/nextjs-tutorialis better than/blog/123 - Use kebabโcase โ hyphen separated slugs
- Avoid query params for main content โ prefer
params - Keep URLs short & readable
๐ฏ Summary Mental Model
[slug]โ Single dynamic value[...slug]โ Unlimited depth (Array)[[...slug]]โ Optional depth (Array orundefined)- Next.js 16 โ Always await params in Server Components
๐ Next Lesson
Layouts Deep Dive โ layout.jsx vs template.jsx and why one persists while the other resets.