0Pricing
Next.js 15 Fullstack (App Router + Server Actions) · Leçon

Routes dynamiques et paramètres

Apprenez à implémenter des routes dynamiques pour gérer les segments variables des URL et accéder aux paramètres de route.

Routes dynamiques et paramètres est une leçon Next.js 15 Fullstack (App Router + Server Actions) gratuite sur CoddyKit. Ceci est la leçon 2 sur 4. Tu peux lire la leçon complète ci-dessous gratuitement — puis la pratiquer en direct dans le navigateur avec un éditeur de code intégré et un tuteur IA 24/7. Elle fait partie du parcours d'apprentissage Next.js 15 Fullstack (App Router + Server Actions), et ta progression se synchronise sur le web et l'application CoddyKit. Le cours Next.js 15 Fullstack (App Router + Server Actions) comprend 4 leçons au total.

Certaines parties de cette leçon n'ont pas encore été traduites et s'affichent en anglais.

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.slug is ['a']
  • /docs/a/b/c -> params.slug is ['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 params prop in your page.js components.

This powerful feature is key for building flexible and scalable web applications!

Questions Fréquemment Posées

La leçon « Routes dynamiques et paramètres » est-elle gratuite ?

Oui — le texte complet de « Routes dynamiques et paramètres » est gratuit à lire ici sur le web. Pour la pratiquer de manière interactive (un éditeur de code intégré et un tuteur IA 24/7) et déverrouiller le reste du cours Next.js 15 Fullstack (App Router + Server Actions), passe à CoddyKit PRO. Le cours Next.js 15 Fullstack (App Router + Server Actions) comprend 4 leçons au total.

Qu'est-ce que j'apprendrai dans « Routes dynamiques et paramètres » ?

Apprenez à implémenter des routes dynamiques pour gérer les segments variables des URL et accéder aux paramètres de route. Tu pratiques Next.js 15 Fullstack (App Router + Server Actions) avec du code pratique que tu exécutes directement dans le navigateur, et un tuteur IA 24/7 répond à tes questions au fur et à mesure que tu avances dans la leçon.

Dois-je avoir de l'expérience pour commencer Next.js 15 Fullstack (App Router + Server Actions) ?

Aucune expérience préalable n'est requise. Next.js 15 Fullstack (App Router + Server Actions) sur CoddyKit est structuré pour les débutants jusqu'aux apprenants avancés, donc tu peux commencer ici ou depuis le début et avancer à ton rythme. Ceci est la leçon 2 sur 4.

Combien de temps prend la leçon « Routes dynamiques et paramètres » ?

La plupart des leçons CoddyKit prennent environ 5–10 minutes. Chacune est courte et interactive, tu progresses régulièrement et tu repiques exactement où tu t'es arrêté sur le web et l'app.

Peux-tu écrire et exécuter du code dans cette leçon Next.js 15 Fullstack (App Router + Server Actions) ?

Oui. Chaque leçon Next.js 15 Fullstack (App Router + Server Actions) inclut un éditeur de code intégré, tu écris et exécutes du vrai code directement dans ton navigateur et tu reçois des retours IA instantanés — aucune configuration locale requise.

Toutes les leçons de ce cours

  1. Mises en page et composants de page
  2. Routes dynamiques et paramètres
  3. Interfaces de chargement et d’erreur
  4. Groupes de routes et mises en page imbriquées
← Retour à Next.js 15 Fullstack (App Router + Server Actions)