πŸ”€ Dynamic Routes in Next.js v15 – Building Pages Like /blog/post-1, /blog/post-2, and Beyond

By now, you’ve learned how to create pages and subpages in Next.js using the app/ folder and page.js files.

But what if you’re building something like a blog with many blog posts?

Do you really need to create a new folder and file for every blog post?

Nope! πŸŽ‰
That’s where dynamic routes come in.

Let’s explore how they work in Next.js v15 πŸ‘‡


πŸ“˜ The Problem: Static Folders Don't Scale

Let’s say you want to build this:

/blog/post-1  
/blog/post-2  
/blog/post-3  
...

You might think:
β€œI’ll just create folders for each post…”

app/
β”œβ”€β”€ blog/
β”‚   β”œβ”€β”€ post-1/
β”‚   β”‚   └── page.js
β”‚   β”œβ”€β”€ post-2/
β”‚   β”‚   └── page.js

🚨 But this is not scalable!

What if you have hundreds of posts, or they come from a database?

You need a single reusable route β€” and that’s where dynamic routes shine.


🧩 Solution: Dynamic Folder with Square Brackets

To create a dynamic route in Next.js, use square brackets in your folder name.

app/
β”œβ”€β”€ blog/
β”‚   β”œβ”€β”€ [slug]/
β”‚   β”‚   └── page.js
  • [slug] is just a placeholder name (you can name it anything)

  • page.js renders the same layout for every /blog/:slug route

βœ… This single route now handles:

  • /blog/post-1

  • /blog/post-2

  • /blog/my-first-article

  • /blog/anything-you-want


πŸ› οΈ Step-by-Step: Create Dynamic Blog Posts

1. Create the Main Blog Page

app/blog/page.js
import Link from 'next/link';

export default function BlogPage() {
  return (
    <main>
      <h1>The Blog</h1>
      <p>
        <Link href="/blog/post-1">Read Post 1</Link>
      </p>
      <p>
        <Link href="/blog/post-2">Read Post 2</Link>
      </p>
    </main>
  );
}

2. Create the Dynamic Route

app/blog/[slug]/page.js
export default function BlogPostPage({ params }) {
  return (
    <main>
      <h1>Blog Post</h1>
      <p>You are viewing: {params.slug}</p>
    </main>
  );
}

βœ… params.slug is automatically passed by Next.js
βœ… It contains whatever value is in the URL (post-1, post-2, etc.)


🀯 How Does This Work?

Next.js looks at your folder like this:

app/blog/[slug]/page.js

And understands that [slug] is a dynamic segment.

  • If you go to /blog/hello-world β†’ params.slug = "hello-world"

  • If you go to /blog/post-23 β†’ params.slug = "post-23"


🧠 Where Does params Come From?

You might wonder:

"Who gives us params as a prop?"

Great question!

  • You don’t pass it manually

  • Next.js automatically provides it when rendering a dynamic route

  • Inside your component, you can access it directly using destructuring:

export default function BlogPostPage({ params }) {
  return <p>{params.slug}</p>;
}

🧡 Real-World Use: Fetching Content by Slug

In real apps, you'd use params.slug to fetch blog post data from a database or CMS:

const postData = await fetchPostBySlug(params.slug);

Then render the title, content, and author dynamically.

βœ… One route
βœ… Infinite pages
βœ… No extra code or folders for every post


πŸ“Œ Recap: Dynamic Routes in a Nutshell

FeatureDescription
[slug] folderCreates a dynamic segment
params.slugHolds the actual URL value
page.jsStill required inside the [slug] folder
BenefitsReusable, scalable, clean routing
0
Subscribe to my newsletter

Read articles from Muhammad Sufiyan directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Muhammad Sufiyan
Muhammad Sufiyan

As a former 3D Animator with more than 12 years of experience, I have always been fascinated by the intersection of technology and creativity. That's why I recently shifted my career towards MERN stack development and software engineering, where I have been serving since 2021. With my background in 3D animation, I bring a unique perspective to software development, combining creativity and technical expertise to build innovative and visually engaging applications. I have a passion for learning and staying up-to-date with the latest technologies and best practices, and I enjoy collaborating with cross-functional teams to solve complex problems and create seamless user experiences. In my current role as a MERN stack developer, I have been responsible for developing and implementing web applications using MongoDB, Express, React, and Node.js. I have also gained experience in Agile development methodologies, version control with Git, and cloud-based deployment using platforms like Heroku and AWS. I am committed to delivering high-quality work that meets the needs of both clients and end-users, and I am always seeking new challenges and opportunities to grow both personally and professionally.