#App Router: dynamic route segment

1 messages · Page 1 of 1 (latest)

lost pebble
#

Can I do [slug]/route.js?

If so, how do I write the route handler

subtle chasmBOT
#

Hi @lost pebble. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Yes, you can use a dynamic segment with a Route Handler in the App Router, e.g.:

app/[slug]/route.js or app/[slug]/route.ts [Dynamic route; route.js ref]

A minimal example:

// app/[slug]/route.ts
export async function GET(
  request: Request,
  { params }: { params: Promise<{ slug: string }> }
) {
  const { slug } = await params

  // Do something with slug, e.g. fetch data
  return Response.json({ slug })
}

For multiple params:

// app/shop/[tag]/[item]/route.ts
export async function GET(
  request: Request,
  { params }: { params: Promise<{ tag: string; item: string }> }
) {
  const { tag, item } = await params
  return Response.json({ tag, item })
}

For a catch‑all segment:

// app/blog/[...slug]/route.ts
export async function GET(
  request: Request,
  { params }: { params: Promise<{ slug: string[] }> }
) {
  const { slug } = await params
  return Response.json({ slug })
}

These follow the documented pattern for dynamic Route Handlers. [route dynamic]

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: