Next.js/Routing/Lesson 09
Dynamic Routing — [slug] · [...slug] · generateStaticParams
35 min·theory
This chapter
1/3
TypeScript
Dynamic Routing — [slug] · [...slug] · generateStaticParams
💡 Why Learn This? — URL Patterns Are Folder Structures
🎯
Dynamic segments are essential for handling URLs like /users/1, /users/2, and /users/3 with a single page file.
💼
The Pages Router pattern of `[slug].tsx` + `getStaticPaths` becomes cleaner in the App Router as `[slug]/page.tsx` + `generateStaticParams`.
⚡
Understanding the difference between `[...slug]` (catch-all) and `[[...slug]]` (optional catch-all) lets you handle multi-level paths like `/docs/getting-started/intro` with a single file.
🔗
`params` is always a string (or string[]) — TypeScript tells you exactly. If you forget to convert to a number, the compiler will catch it.
🏢 실무에서는
codemaster40 itself is built with a double dynamic route `app/study/[category]/[slug]/page.tsx`. A single URL like `/study/javascript/promise` is handled by one file, and at build time `generateStaticParams` pre-generates all possible (category, slug) combinations statically — giving you both SEO and performance.
3 Types of Dynamic Segments + generateStaticParams
1. Single Dynamic Segment — [slug]
2. Catch-all — [...slug]
Note: catch-all requires at least one segment. /docs alone will not match.
3. Optional Catch-all — [[...slug]]
4. generateStaticParams — Static Generation at Build Time
- ▸Fetches the list of possible slugs at build time and generates static HTML.
- ▸Serves the same purpose as
getStaticPathsin the Pages Router. - ▸Slugs not returned are SSR-rendered on first request and cached (dynamicParams defaults to true).
5. Nested Dynamic Segments — [category]/[slug]
💡 💡 Dynamic Routing in Practice — 5 Tips
1. params are always strings — convert to number yourself when needed
2. [...slug] is an array; [slug] is a string
3. generateStaticParams must return an array of objects
4. Set dynamicParams = false to allow only pre-generated paths
Defaults to true (runtime SSR + caching).
5. Place loading.tsx · error.tsx · not-found.tsx in the same folder
Loading, error, and not-found UIs for dynamic pages are inherited automatically. Covers both SEO and UX.
⚡ Try It Yourself — Route Pattern Matching
Simulate how different dynamic patterns match various URLs.
✏️ JS 코드
📟 Console output
▶ Press the Run button
⚠️ Runs in a browser sandbox — only console.log() is supported; alert/fetch are not.
Check Your Understanding
What is the key difference between `app/docs/[...slug]/page.tsx` and `app/docs/[[...slug]]/page.tsx`?
Read this first: Optimistic UI — useActionState + useOptimistic