동적 경로와 포괄 세그먼트
동적 세그먼트와 포괄 경로를 사용하여 동적 콘텐츠에 맞게 유연하게 동작하는 경로를 만듭니다.
동적 경로와 포괄 세그먼트은(는) CoddyKit의 무료 Next.js 15 Fullstack Web Apps 강의입니다. 이것은 4개 중 1번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 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-worldor/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. slugwill 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:
/docsand/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 beingundefinedfor the base path.
These powerful patterns are fundamental for building scalable and content-rich applications in Next.js.
자주 묻는 질문
“동적 경로와 포괄 세그먼트” 강의는 무료인가요?
네 — “동적 경로와 포괄 세그먼트” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Next.js 15 Fullstack Web Apps 강의 전체를 잠금 해제할 수 있습니다. Next.js 15 Fullstack Web Apps 강의에는 총 4개의 강의가 포함되어 있습니다.
“동적 경로와 포괄 세그먼트”에서 뭘 배우나요?
동적 세그먼트와 포괄 경로를 사용하여 동적 콘텐츠에 맞게 유연하게 동작하는 경로를 만듭니다. 브라우저에서 직접 실행하는 실습 코드로 Next.js 15 Fullstack Web Apps을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Next.js 15 Fullstack Web Apps을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Next.js 15 Fullstack Web Apps은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 1번째 강의입니다.
“동적 경로와 포괄 세그먼트” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Next.js 15 Fullstack Web Apps 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Next.js 15 Fullstack Web Apps 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- 동적 경로와 포괄 세그먼트
- 중첩 레이아웃과 경로 그룹
- 병렬 경로와 가로채기 경로
- 로딩 및 오류 UI 규칙