Advanced routing
Advanced routing in the Next.js App Router: dynamic routes so one page serves thousands of URLs, catch-all routes, route groups and private folders that organise code without changing the URL, and parallel + intercepting routes that build an Instagram-style shareable modal.
60.1 Problem: a folder per product?
- One folder + page file per phone is technically correct: every folder becomes a URL segment.
- But all page files are almost identical; creating 10,000 folders for 10,000 products is not practical.
app/└─ products/ ├─ iphone-16/ │ └─ page.jsx ├─ galaxy-s25/ │ └─ page.jsx ├─ pixel-10/ │ └─ page.jsx └─ ... 9997 more folders?60.2 Fix: a dynamic route [id]
- Write the folder name in square brackets: app/products/[id]/page.jsx.
- Whatever value comes after /products/ in the URL becomes id, so one page handles every product.
- Next.js passes a params prop; in Next.js 16 params is a Promise, so await params.
- params are known only when the request arrives, so keep a loading file in the dynamic folder, otherwise the Next.js 16 build fails.
// app/products/[id]/page.jsxexport default async function ProductPage({ params,}) { const { id } = await params; return ( <main> <h1>Product {id}</h1> <p>Showing details for {id}</p> </main> );}URL: /products/42# Product 42Showing details for 42 URL: /products/pixel-10# Product pixel-10Showing details for pixel-10- Step 1
1.Request: /products/42
- Step 2
2.Next.js matches app/products/[id]/page.jsx
- Step 3
3.params = Promise of { id: "42" }
- Step 4
4.const { id } = await params
- Step 5
5.Page renders Product 42 on the server
60.3 Checkpoint: what type is id?
- Every param from the URL is a string: /products/42 gives the string "42".
- If the id is a number in the database, convert it with Number(id).
- The log appears in the terminal (the page runs on the server); it can appear twice because Next.js 16 renders once more in dev to check it.
// app/products/[id]/page.jsxexport default async function ProductPage({ params,}) { const { id } = await params; console.log(typeof id, id); return <h1>Product {id}</h1>;}$ npm run devstring 42string 4260.4 Catch-all route [...slug]
- For URLs with any number of levels, like /docs/react/hooks or /docs/react/hooks/use-state.
- Three dots before slug in square brackets; slug is an array of all segments in order.
- One page handles both URLs.
// app/docs/[...slug]/page.jsxexport default async function DocsPage({ params,}) { const { slug } = await params; // /docs/react/hooks → ["react", "hooks"] return <h1>Docs: {slug.join(" > ")}</h1>;}URL: /docs/react/hooks# Docs: react > hooks URL: /docs/react/hooks/use-state# Docs: react > hooks > use-state| Folder | Matches | /docs | /docs/react | /docs/react/hooks | params value |
|---|---|---|---|---|---|
| [id] | Exactly 1 segment | No | Yes | No | "react" (string) |
| [...slug] | 1 or more segments | No | Yes | Yes | ["react", "hooks"] |
| [[...slug]] | 0 or more segments | Yes | Yes | Yes | undefined on /docs |
60.5 Route groups and private folders
- Route group: folder name in round brackets, like (marketing) and (shop). Next.js does not count it in the URL.
- Each group can have its own layout: /about uses the marketing layout, /cart uses the shop layout.
- Private folder: name starts with an underscore (_components); it never becomes a route, perfect for components.
app/├─ (marketing)/│ ├─ layout.jsx│ ├─ page.jsx → /│ └─ about/page.jsx → /about├─ (shop)/│ ├─ layout.jsx│ └─ cart/page.jsx → /cart└─ _components/ └─ Navbar.jsx (no route)60.6 Parallel routes: slots
- For one screen with independent sections (analytics, team), each with its own data and loading state.
- Create folders starting with @ inside dashboard (@analytics, @team): these are slots.
- Next.js passes each slot to the layout as a prop with the same name; render them next to children.
// app/dashboard/layout.jsx// folders: @analytics and @teamexport default function DashboardLayout({ children, analytics, team,}) { return ( <div className="grid"> {children} {analytics} {team} </div> );}URL: /dashboard# DashboardAnalytics: 1240 visitsTeam: 8 members60.7 Intercepting route: photo modal
- Instagram style: click a photo in the feed, the URL becomes /photo/7, but the photo opens in a modal with the feed behind it.
- Inside the @modal slot, (.)photo/[id] intercepts the photo route at the same level.
- On refresh (or a shared link) the normal full photo page opens; @modal/default.jsx returns null.
app/├─ layout.jsx → {children}{modal}├─ page.jsx → feed├─ photo/[id]/│ └─ page.jsx → full photo page└─ @modal/ ├─ default.jsx → return null └─ (.)photo/[id]/ └─ page.jsx → photo in a modal- Step 1
1.User clicks a Link in the feed: client-side navigation, URL becomes /photo/7
- Step 2
2.(.)photo/[id] in the @modal slot intercepts the navigation
- Step 3
3.Photo shows in a modal, feed stays behind it
- Step 4
4.Refresh or shared link: not intercepted
- Step 5
5.Full photo page from app/photo/[id]/page.jsx loads
Together, parallel + intercepting routes give a modal with a shareable URL
60.8 Comparison: folder naming conventions
| Convention | Example | In the URL? | Used for |
|---|---|---|---|
| [param] | [id] | Yes, as a value | Dynamic route |
| [...param] | [...slug] | Yes, 1+ segments | Catch-all |
| [[...param]] | [[...slug]] | Yes, 0+ segments | Optional catch-all |
| (group) | (shop) | No | Organising, separate layouts |
| _folder | _components | No (never a route) | Private code |
| @slot | @modal | No | Parallel routes |
| (.)folder | (.)photo | Shows the intercepted URL | Intercepting routes |
60.9 Common mistakes
- Reading params without await (from old tutorials): worked until Next.js 14, but in Next.js 16 params is a Promise, so id is undefined. Same rule for searchParams.
- Thinking every folder is part of the URL: (groups), _private folders and @slots never appear in it, and a folder is a public route only with a page or route file.
- Two pages with the same URL in different route groups: build error.
- A parallel slot without default.jsx: Next.js 16 fails the build.
// Wrong: Next.js 16, id is undefinedexport default function ProductPage({ params,}) { const { id } = params; return <h1>Product {id}</h1>;}// Right: id is "42"export default async function ProductPage({ params,}) { const { id } = await params; return <h1>Product {id}</h1>;}60.10 Best practices
- Always await params and searchParams; their values are strings.
- Never create the same URL in two route groups.
- Keep a default file in every parallel slot.
- Build Link URLs with a template literal: `/products/${id}`.
60.11 Interview tips
- Dynamic route [id]: one page, thousands of URLs; in Next.js 16 params is a Promise and values are strings.
- [...slug] gives an array of segments; [[...slug]] also matches the base URL.
- Parallel routes: @slots render multiple pages together in one layout.
- Intercepting routes: show another route inside the current layout during client navigation.
- Together they build an Instagram-style modal that is shareable and opens as a full page on refresh.