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
Apage file makes a route segment publicly accessible:
Layouts
Alayout file defines UI that is shared across multiple pages. Layouts preserve state, remain interactive, and do not re-render on navigation.
app/layout.tsx is required and must contain <html> and <body> tags.
Nested layouts
Layouts are nested automatically. A layout atapp/blog/layout.tsx wraps all pages inside app/blog/:
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:
[[...slug]] (double brackets) for optional catch-all, which also matches the root /docs.
generateStaticParams
UsegenerateStaticParams 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:- 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 thesearchParams prop:
searchParams opts the page into dynamic rendering. In Client Components, use the useSearchParams hook instead.
Navigation
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.tsxis present
Programmatic navigation
UseuseRouter in Client Components for programmatic navigation:
Route type helpers
Next.js generates utility types that inferparams and named slots from your route structure:
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:middleware.ts at the root of your project (same level as app/). The matcher config controls which paths trigger the middleware.
