Instructions for Next.js 16 App Router
When assisting with Next.js 16 App Router tasks:
- •
Understand the Structure: The App Router uses a file-system-based routing in the
app/directory. Folders represent route segments, and files likepage.tsxdefine the UI for that route. - •
Basic Route Setup:
- •Create a folder in
app/for each route segment (e.g.,app/about/for/about). - •Add a
page.tsxfile exporting a default React component.
- •Create a folder in
- •
Dynamic Routes:
- •Use brackets for dynamic segments:
app/posts/[id]/page.tsx. - •Access params in the component:
export default function Post({ params }: { params: { id: string } }) { ... }.
- •Use brackets for dynamic segments:
- •
Layouts:
- •Use
layout.tsxin route groups or root to wrap child routes. - •Root layout at
app/layout.tsxmust include<html>and<body>.
- •Use
- •
Navigation:
- •Use
Linkfromnext/linkfor client-side navigation. - •For programmatic navigation, use
useRouterfromnext/navigation.
- •Use
- •
Loading and Error States:
- •Add
loading.tsxfor suspense boundaries. - •Add
error.tsxfor error handling.
- •Add
- •
Route Groups:
- •Use parentheses for grouping without affecting URL:
app/(auth)/login/page.tsx.
- •Use parentheses for grouping without affecting URL:
- •
Parallel Routes:
- •Use slots with
@prefix:app/@analytics/page.tsx.
- •Use slots with
- •
Best Practices:
- •Prefer Server Components where possible.
- •Handle data fetching with
fetchor third-party libraries. - •Ensure TypeScript types for params and search params.
References
Use the shared references located at: ../_shared/reference.md