C.W.K.
Stream
Lesson 05 of 10 · published

Dynamic Route

~22 min · dynamic segment, catch-all, optional

Level 0Curious
0 XP0/68 lessons0/11 achievements
0/120 XP to next level120 XP to go0% complete

Dynamic segment 세 가지

Folder pattern예 URLparams 모양
[slug]/blog/hello{ slug: 'hello' }
[...slug]/docs/a/b/c{ slug: ['a','b','c'] }
[[...slug]]/docs 또는 /docs/a/b{ slug: undefined } 또는 { slug: ['a','b'] }

여러 segment 깔끔하게 합쳐

한 path 안에 dynamic segment 여러 개 가능. params 가 모든 걸 한 객체로 합친 모양이야.

404 vs render

Request 가 /blog/foo 로 들어왔는데 generateStaticParams 가 미리 build 안 했으면, default 는 on demand render. Unknown slug 가 404 이게 하려면 export const dynamicParams = false.

Code

Optional catch-all·tsx
// app/docs/[[...slug]]/page.tsx
type Props = { params: Promise<{ slug?: string[] }> };

export default async function DocsPage({ params }: Props) {
  const { slug } = await params;
  if (!slug) return <DocsIndex />;

  const path = slug.join('/'); // 'getting-started/install'
  const doc = await getDoc(path);
  if (!doc) notFound();
  return <DocContent doc={doc} />;
}
두 dynamic segment·tsx
// app/[locale]/blog/[slug]/page.tsx
import { notFound } from 'next/navigation';

type Props = { params: Promise<{ locale: string; slug: string }> };

export default async function Page({ params }: Props) {
  const { locale, slug } = await params;
  const post = await getPost(locale, slug);
  if (!post) notFound();
  return <Article post={post} />;
}
Unknown slug 강제 404·tsx
// app/blog/[slug]/page.tsx
export const dynamicParams = false;

export async function generateStaticParams() {
  const slugs = await getAllPostSlugs();
  return slugs.map(slug => ({ slug }));
}

External links

Exercise

app/docs/[[...slug]]/page.tsx 로 docs route 만들어 — /docs (index) 와 /docs/a/b/c (nested doc) 둘 다 서빙. Known slug 3 개에 generateStaticParams 추가하고 dynamicParams = false 가 unknown 을 404 시키는지 확인.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.