Skip to main content
Next.js uses file-system based routing. Folders and files inside the app directory map directly to URL paths.

File-system routing

The App Router defines routes using two conventions:
  • Folders define the route segments that map to URL segments
  • Files (page.tsx, layout.tsx, etc.) create UI that is shown for a segment

Pages

A page file makes a route segment publicly accessible:

Layouts

A layout file defines UI that is shared across multiple pages. Layouts preserve state, remain interactive, and do not re-render on navigation.
The root layout at app/layout.tsx is required and must contain <html> and <body> tags.

Nested layouts

Layouts are nested automatically. A layout at app/blog/layout.tsx wraps all pages inside app/blog/:
The rendering hierarchy becomes: RootLayout → BlogLayout → Page.

Dynamic routes

Wrap a folder name in square brackets to create a dynamic segment:
params is a Promise in the App Router. Always await it before accessing its values.

Catch-all segments

Use [...slug] to match multiple segments:
Use [[...slug]] (double brackets) for optional catch-all, which also matches the root /docs.

generateStaticParams

Use generateStaticParams to statically generate dynamic routes at build time:

Route groups

Wrap a folder name in parentheses to create a route group. Route groups organize routes without affecting the URL:
Route groups are useful for:
  • Applying different layouts to different sections without changing URLs
  • Organizing large codebases by feature or team
  • Creating multiple root layouts

Special files

The App Router has several reserved file names:

page.tsx

Makes a route segment publicly accessible. Receives params and searchParams props.

layout.tsx

Shared UI that wraps child pages and layouts. Does not re-render on navigation.

loading.tsx

Instant loading state shown while a route segment loads. Automatically wraps the segment in <Suspense>.

error.tsx

Error UI for a route segment. Must be a Client Component.

not-found.tsx

UI shown when notFound() is called inside a segment.

route.tsx

API endpoint for the segment. Equivalent to API Routes in the Pages Router.

Search params

Access search parameters in a Server Component page via the searchParams prop:
Using searchParams opts the page into dynamic rendering. In Client Components, use the useSearchParams hook instead. Use the <Link> component for client-side navigation with automatic prefetching:
<Link> automatically prefetches routes when they enter the viewport:
  • Static routes: the full route is prefetched
  • Dynamic routes: prefetching is skipped or partial if loading.tsx is present

Programmatic navigation

Use useRouter in Client Components for programmatic navigation:

Route type helpers

Next.js generates utility types that infer params and named slots from your route structure:
These types are globally available — no imports required. They’re generated during next dev, next build, or next typegen.

Middleware

Middleware runs before a request is completed and allows you to rewrite, redirect, or modify the request:
Place middleware.ts at the root of your project (same level as app/). The matcher config controls which paths trigger the middleware.
Middleware runs on every matched request. Keep it lightweight — avoid expensive operations like database queries.