SkillPilotAI

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.
Output
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.
React (JSX)
// 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>  );}
Output
URL: /products/42# Product 42Showing details for 42 URL: /products/pixel-10# Product pixel-10Showing details for pixel-10
How a dynamic route resolves
  1. 1.Request: /products/42

  2. 2.Next.js matches app/products/[id]/page.jsx

  3. 3.params = Promise of { id: "42" }

  4. 4.const { id } = await params

  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.
React (JSX)
// app/products/[id]/page.jsxexport default async function ProductPage({  params,}) {  const { id } = await params;  console.log(typeof id, id);   return <h1>Product {id}</h1>;}
Output
$ npm run devstring 42string 42

60.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.
React (JSX)
// 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>;}
Output
URL: /docs/react/hooks# Docs: react > hooks URL: /docs/react/hooks/use-state# Docs: react > hooks > use-state
FolderMatches/docs/docs/react/docs/react/hooksparams value
[id]Exactly 1 segmentNoYesNo"react" (string)
[...slug]1 or more segmentsNoYesYes["react", "hooks"]
[[...slug]]0 or more segmentsYesYesYesundefined 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.
Output
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.
React (JSX)
// app/dashboard/layout.jsx// folders: @analytics and @teamexport default function DashboardLayout({  children,  analytics,  team,}) {  return (    <div className="grid">      {children}      {analytics}      {team}    </div>  );}
Output
URL: /dashboard# DashboardAnalytics: 1240 visitsTeam: 8 members

60.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.
Output
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
The photo modal flow
  1. 1.User clicks a Link in the feed: client-side navigation, URL becomes /photo/7

  2. 2.(.)photo/[id] in the @modal slot intercepts the navigation

  3. 3.Photo shows in a modal, feed stays behind it

  4. 4.Refresh or shared link: not intercepted

  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

ConventionExampleIn the URL?Used for
[param][id]Yes, as a valueDynamic route
[...param][...slug]Yes, 1+ segmentsCatch-all
[[...param]][[...slug]]Yes, 0+ segmentsOptional catch-all
(group)(shop)NoOrganising, separate layouts
_folder_componentsNo (never a route)Private code
@slot@modalNoParallel routes
(.)folder(.)photoShows the intercepted URLIntercepting 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.
React (JSX)
// Wrong: Next.js 16, id is undefinedexport default function ProductPage({  params,}) {  const { id } = params;  return <h1>Product {id}</h1>;}
React (JSX)
// 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.