How does file-based routing work in Next.js?

In the App Router, each folder inside app/ is a URL segment, and a route becomes publicly reachable only when that folder contains a page.tsx (UI) or route.ts (API). Brackets, parentheses and other folder-name conventions add dynamic and organizational behavior.

Basic mapping

  • app/page.tsx serves /.
  • app/about/page.tsx serves /about.
  • app/blog/[slug]/page.tsx serves /blog/anything.
  • app/api/users/route.ts serves /api/users for HTTP methods.

A folder without page or route is not a route, so you can safely colocate components, tests and utilities next to it.

Folder conventions

  • [id] dynamic segment, [...slug] catch-all, [[...slug]] optional catch-all.
  • (group) route group: organizes files or shares a layout without adding to the URL.
  • _folder private folder: excluded from routing entirely.
  • @slot parallel route slot rendered by the parent layout.
  • (.)photo intercepting route, for example opening a modal over the current page.

Special files in a segment

layout, page, loading, error, not-found, template, default and route. They nest: a page renders inside every parent layout, wrapped by the nearest loading and error boundaries.

Pages Router comparison

In pages/, every file is a route (pages/about.tsx serves /about), so you cannot colocate non-route files there without them becoming pages.

Type safety

Run next typegen (or just next dev/next build) to generate global helpers such as PageProps<'/blog/[slug]'> and LayoutProps. Setting typedRoutes: true makes <Link href> type-checked against real routes.

Code example

tsx
1// app/
2// ├── page.tsx -> /
3// ├── (marketing)/ -> group, not in URL
4// │ ├── layout.tsx shared marketing layout
5// │ └── pricing/page.tsx -> /pricing
6// ├── blog/
7// │ ├── page.tsx -> /blog
8// │ ├── _components/PostCard.tsx private, never a route
9// │ └── [slug]/page.tsx -> /blog/:slug
10// ├── docs/[[...path]]/page.tsx -> /docs, /docs/a, /docs/a/b
11// └── api/health/route.ts -> GET /api/health
12
13// app/blog/[slug]/page.tsx
14export default async function Post({ params }: PageProps<"/blog/[slug]">) {
15 const { slug } = await params; // params is a Promise since Next.js 15
16 return <h1>Post: {slug}</h1>;
17}
18
19// app/api/health/route.ts
20export function GET() {
21 return Response.json({ ok: true });
22}

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the Next.js set.

Open in study app