Dynamic Routes & Params
Learn to implement dynamic routes to handle variable segments in URLs and access route parameters.
Dynamic Routes & Params is a free Next.js 15 Fullstack (App Router + Server Actions) lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Next.js 15 Fullstack (App Router + Server Actions) learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What are Dynamic Routes?
Imagine you have a blog with many posts. Instead of creating a separate page for each post (like /blog/post-1, /blog/post-2), you can use dynamic routes.
Dynamic routes allow you to create pages that respond to variable segments in the URL, making your application scalable and maintainable.
Defining a Basic Dynamic Route
In Next.js App Router, you define a dynamic route segment by wrapping a folder name in square brackets []. For example, to create a dynamic route for blog posts, you'd create a folder named [slug].
app/blog/[slug]/page.js
This structure will match URLs like /blog/first-post, /blog/another-article, etc.
Accessing Route Parameters
When a dynamic route is matched, the variable part of the URL becomes available as a route parameter. You can access these parameters within your page component via the params prop.
For app/blog/[slug]/page.js, the URL /blog/my-first-post would make params.slug equal to 'my-first-post'.
Demo: Simple Dynamic Page
Let's see how to create a simple dynamic page that displays the slug parameter from the URL. Place this code in app/blog/[slug]/page.js.
export default function BlogPostPage({ params }) {
// params.slug will contain the dynamic part of the URL
return (
<div>
<h1>Blog Post: {params.slug}</h1>
<p>This content is dynamically loaded!</p>
</div>
);
}Understanding Catch-all Segments
What if you need to match a path with an unknown number of segments? This is where catch-all segments come in handy. You define them using a spread syntax: [...slug].
app/docs/[...slug]/page.js
This will match URLs like /docs/a, /docs/a/b, /docs/a/b/c, etc.
Accessing Catch-all Parameters
When using a catch-all segment, the params prop will contain an array of the matched segments. So, for app/docs/[...slug]/page.js:
/docs/a->params.slugis['a']/docs/a/b/c->params.slugis['a', 'b', 'c']
Demo: Catch-all Page
Here's how you can display all segments from a catch-all route. Place this code in app/docs/[...slug]/page.js.
export default function DocsPage({ params }) {
// params.slug is an array of path segments
const path = params.slug.join('/');
return (
<div>
<h1>Documentation Path: /{path}</h1>
<p>Showing content for: {path}</p>
</div>
);
}Optional Catch-all Segments
Sometimes you want a catch-all segment that can also match the base path (i.e., zero segments). This is an optional catch-all, defined with double square brackets: [[...slug]].
app/users/[[...id]]/page.js
This will match /users (where params.id is undefined) AND /users/123 (where params.id is ['123']).
Demo: Optional Catch-all
This example shows an optional catch-all route that displays a user ID or 'Guest' if no ID is provided. Place this in app/users/[[...id]]/page.js.
export default function UserProfilePage({ params }) {
// params.id will be undefined for /users, or ['123'] for /users/123
const userId = params.id ? params.id[0] : 'Guest';
return (
<div>
<h1>Welcome, {userId}</h1>
<p>This page handles both base and dynamic paths.</p>
</div>
);
}Dynamic Routes Quiz
Which of the following file structures would allow a Next.js App Router application to handle URLs like /products/electronics/laptops and access 'electronics' and 'laptops' separately?
Recap: Dynamic Routes & Params
Great job! You've learned the essentials of dynamic routing in Next.js App Router:
[slug]creates a single dynamic segment.[...slug]creates a catch-all segment for multiple paths, returning an array.[[...slug]]creates an optional catch-all, matching zero or more segments.- All dynamic segments are accessed via the
paramsprop in yourpage.jscomponents.
This powerful feature is key for building flexible and scalable web applications!
Frequently asked questions
Is the “Dynamic Routes & Params” lesson free?
Yes — the full text of “Dynamic Routes & Params” is free to read here on the web, and the Next.js 15 Fullstack (App Router + Server Actions) course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Next.js 15 Fullstack (App Router + Server Actions) course, upgrade to CoddyKit PRO.
What will I learn in “Dynamic Routes & Params”?
Learn to implement dynamic routes to handle variable segments in URLs and access route parameters. You practise Next.js 15 Fullstack (App Router + Server Actions) with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Next.js 15 Fullstack (App Router + Server Actions)?
No prior experience is required. Next.js 15 Fullstack (App Router + Server Actions) on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Dynamic Routes & Params” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Next.js 15 Fullstack (App Router + Server Actions) lesson?
Yes. Every Next.js 15 Fullstack (App Router + Server Actions) lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Layouts and Page Components
- Dynamic Routes & Params
- Loading & Error UI
- Route Groups and Nested Layouts