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.