π 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
Feature | Description |
[slug] folder | Creates a dynamic segment |
params.slug | Holds the actual URL value |
page.js | Still required inside the [slug] folder |
Benefits | Reusable, scalable, clean routing |
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.