0Pricing
Next.js 15 Fullstack Web Apps · レッスン

動的ルートとキャッチオールセグメント

動的セグメントとキャッチオールルートを使って、動的なコンテンツに対応する柔軟なルートを作成します。

「動的ルートとキャッチオールセグメント」はCoddyKit上の無料Next.js 15 Fullstack Web Appsレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはNext.js 15 Fullstack Web Apps学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Next.js 15 Fullstack Web Appsコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Flexible Routing in Next.js

Welcome to dynamic routing in Next.js! Modern web applications often need flexible URLs for content like blog posts, product details, or user profiles.

Instead of creating a separate page for each item, dynamic routes let you use a single page template to handle many different URLs based on their path.

Creating Dynamic Segments

In Next.js, you create a dynamic route by enclosing a folder name in square brackets, like [slug].

This [slug] segment acts as a placeholder. Whatever value is in that part of the URL will be captured and made available to your page component.

  • Example: app/blog/[slug]/page.js
  • This will match URLs like /blog/hello-world or /blog/nextjs-tips.

Accessing Dynamic Parameters

When a dynamic route is matched, the value of the dynamic segment (e.g., slug) is passed to your page component as a property of the params object.

By default, Next.js App Router pages are React Server Components, which receive params as a prop.

// File: app/blog/[slug]/page.js

export default function BlogPostPage({ params }) {
  const { slug } = params; // If URL is /blog/my-post, slug will be 'my-post'

  return (
    <main>
      <h1>Blog Post: {decodeURIComponent(slug)}</h1>
      <p>This content is for the post identified by '{decodeURIComponent(slug)}'.</p>
    </main>
  );
}

Introducing Catch-all Segments

What if you need to match URLs with an unknown number of segments? For example, a documentation site where paths can be deeply nested (e.g., /docs/getting-started/installation).

This is where catch-all segments come in. They capture all subsequent path segments into an array.

Creating Catch-all Routes

To create a catch-all route, use three dots followed by the segment name inside square brackets: [...slug].

This will match any path that starts with the parent folder and capture all parts after it into an array.

  • Example: app/docs/[...slug]/page.js
  • Matches: /docs/a, /docs/a/b, /docs/a/b/c.
  • slug will be an array: ['a'], ['a', 'b'], ['a', 'b', 'c'].

Accessing Catch-all Parameters

Similar to dynamic segments, catch-all parameters are available via the params object. However, the value for a catch-all segment is always an array of strings.

Let's see an example of displaying the captured path segments from a URL like /docs/guide/setup/nextjs.

// File: app/docs/[...slug]/page.js

export default function DocsPage({ params }) {
  const { slug } = params; // If URL is /docs/intro/setup, slug is ['intro', 'setup']

  return (
    <main>
      <h2>Documentation Path: /{slug.join('/')}</h2>
      <p>Segments found: {slug.map(s => `<code>${s}</code>`).join(', ')}</p>
    </main>
  );
}

Optional Catch-all Segments

What if you want a catch-all route to also match the base path? For example, you want /docs to show an index page, but /docs/intro to show a specific guide.

You can make a catch-all segment optional by wrapping it in an extra set of square brackets: [[...slug]].

  • Example: app/docs/[[...slug]]/page.js
  • Matches: /docs and /docs/a/b.

Optional Catch-all Example

When using [[...slug]], the slug parameter will be an array of strings for sub-paths. However, it will be undefined if only the base path is matched (e.g., /docs).

You'll need to handle the undefined case in your component to show default content.

// File: app/docs/[[...slug]]/page.js

export default function OptionalDocsPage({ params }) {
  const { slug } = params; // If URL is /docs, slug is undefined
                           // If URL is /docs/intro, slug is ['intro']

  const pathDisplay = slug ? slug.join('/') : 'Home Index';

  return (
    <main>
      <h2>Docs: {pathDisplay}</h2>
      <p>This page handles both the base path and any sub-paths.</p>
    </main>
  );
}

Quick Check on Routing

Which of the following statements about Next.js dynamic and catch-all routes are TRUE?

Recap: Dynamic & Catch-all Routes

You've mastered how to make your Next.js application's routing incredibly flexible!

  • Dynamic Segments ([slug]) allow a single page to handle many specific URLs, receiving a string parameter.
  • Catch-all Segments ([...slug]) capture multiple path segments into an array, useful for nested structures.
  • Optional Catch-all Segments ([[...slug]]) extend catch-alls to also match the base path, with the parameter being undefined for the base path.

These powerful patterns are fundamental for building scalable and content-rich applications in Next.js.

よくある質問

「動的ルートとキャッチオールセグメント」レッスンは無料ですか?

はい。「動的ルートとキャッチオールセグメント」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Next.js 15 Fullstack Web Appsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Next.js 15 Fullstack Web Appsコースには全4レッスンが含まれています。

「動的ルートとキャッチオールセグメント」で何を学びますか?

動的セグメントとキャッチオールルートを使って、動的なコンテンツに対応する柔軟なルートを作成します。 ブラウザで直接実行するハンズオンコードでNext.js 15 Fullstack Web Appsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Next.js 15 Fullstack Web Appsを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのNext.js 15 Fullstack Web Appsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。

「動的ルートとキャッチオールセグメント」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このNext.js 15 Fullstack Web Appsレッスンでコードを書いて実行できますか?

はい。すべてのNext.js 15 Fullstack Web Appsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. 動的ルートとキャッチオールセグメント
  2. ネストしたレイアウトとルートグループ
  3. 並列ルートとインターセプトルート
  4. ローディングUIとエラーUIの規約
← Next.js 15 Fullstack Web Appsに戻る