by DannyUpdated 10 sections

Covers the App Router in Next.js 15 and 16.

Download the PDF

Create and run


_10
npx create-next-app@latest my-app
_10
npm run dev # localhost:3000
_10
npm run build # production build
_10
npm start # serve the build

Special files

FilePurpose
page.tsxThe route's UI
layout.tsxShared shell, keeps state
loading.tsxSuspense fallback
error.tsxError boundary (client)
not-found.tsx404 for the segment
route.tsAPI handler

Routes


_10
app/page.tsx /
_10
app/blog/page.tsx /blog
_10
app/blog/[slug]/page.tsx /blog/hello
_10
app/docs/[...all]/page.tsx /docs/a/b
_10
app/(shop)/cart/page.tsx /cart

Dynamic params


_10
type Props = { params: Promise<{ slug: string }> };
_10
_10
export default async function Post({ params }: Props) {
_10
const { slug } = await params;
_10
return <h1>{slug}</h1>;
_10
}
_10
_10
export async function generateStaticParams() {
_10
return [{ slug: 'hello' }];
_10
}

Server and client


_10
// Server component by default: can be async,
_10
// can read the database, ships no JS.
_10
export default async function Page() {
_10
const posts = await db.post.findMany();
_10
return <List posts={posts} />;
_10
}
_10
_10
'use client'; // top of a file that needs
_10
// state, effects or events

Data and caching


_10
await fetch(url, { cache: 'force-cache' });
_10
await fetch(url, { next: { revalidate: 3600 } });
_10
export const revalidate = 3600; // whole route
_10
_10
revalidatePath('/blog'); // after a write
_10
revalidateTag('posts');

Server actions


_10
async function create(formData: FormData) {
_10
'use server';
_10
await db.post.create({
_10
data: { title: String(formData.get('title')) },
_10
});
_10
revalidatePath('/blog');
_10
}
_10
_10
<form action={create}>...</form>

Route handlers


_10
// app/api/items/route.ts
_10
export async function GET(req: Request) {
_10
return Response.json({ items: [] });
_10
}
_10
export async function POST(req: Request) {
_10
const body = await req.json();
_10
return Response.json(body, { status: 201 });
_10
}

Metadata


_10
export const metadata: Metadata = {
_10
title: 'Blog',
_10
description: 'Latest posts',
_10
};
_10
_10
export async function generateMetadata({ params }) {
_10
const { slug } = await params;
_10
return { title: slug };
_10
}