Build SEO Friendly URLs in Next.js πŸš€ Dynamic Routing Deep Dive #7 || Next.js Anatomy || DeathCode - DeathCode

πŸ“‚ 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
  • [id] β†’ Use for internal systems or dashboards

    • Example: /admin/orders/12345

πŸ”₯ 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-anatomy
  • ref β†’ 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-tutorial is 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 or undefined)
  • 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.

Β© 2024 DeathCode. All Rights Reserved.