Overview
Next.js App Router uses a file-based routing system where the file structure in theapp
directory directly maps to URL routes. This approach simplifies routing configuration and provides better performance through automatic code splitting.
Route Segments
Each folder in theapp
directory represents a route segment:
Essential Special Files
page.tsx
- Makes a route publicly accessible
- Required for a route to be accessible
- Defines the UI for that route
layout.tsx
- Shared UI for multiple pages
- Wraps child layouts and pages
- Maintains state during navigation
loading.tsx
- Loading UI for a route segment
- Shows while page is loading
- Automatically wraps page and children
not-found.tsx
- 404 page for a route segment
- Shows when route is not found
- Can be nested at different levels
Basic Navigation
Link Component
Programmatic Navigation
Basic Route Organization
Simple Structure
Nested Routes
Key Takeaway: Next.js file-based routing simplifies route management by
using the file system structure to define routes. Start with basic page.tsx
and layout.tsx files, then add loading.tsx and not-found.tsx as needed.