Next.jsMedium

What is the App Router introduced in Next.js 13?

The App Router is the app/ directory routing system built on React Server Components. It introduced nested layouts, server-first components, streaming and colocated special files, and it is the recommended router for all new Next.js apps.

Timeline

  • Next.js 13 (2022): introduced in beta.
  • 13.4: declared stable.
  • 14: Server Actions became stable.
  • 15: Request APIs (params, cookies(), headers()) became async and fetch stopped caching by default.
  • 16: Turbopack by default, Cache Components ("use cache"), proxy.ts, and synchronous request APIs fully removed.
  • 16.4: new apps enable Cache Components and Partial Prefetching by default.

Core ideas

  1. Server Components by default. Components run on the server and can be async. Add "use client" only for interactivity.
  2. Special files per segment. layout, page, loading, error, not-found, template, default and route.
  3. Nested layouts that persist and keep state across navigations.
  4. Streaming. loading.tsx and <Suspense> let parts of the page render while others wait for data.
  5. Colocation. Components, tests and styles can sit next to routes; only page and route files become public.
  6. Advanced routing. Route groups (name), parallel routes @slot, intercepting routes (.).

Compared to the Pages Router

Data is fetched inside components rather than in getServerSideProps. Mutations use Server Functions rather than API routes. Caching is explicit with "use cache" instead of page-level revalidate. Navigation uses next/navigation hooks (useRouter, usePathname, useSearchParams).

Pitfall

Putting "use client" at the top of a high-level layout turns the whole subtree into client code, losing most App Router benefits. Keep client boundaries at the leaves.

Code example

tsx
1// app/dashboard/layout.tsx - persists across /dashboard/* navigations
2export default function DashboardLayout({ children }: LayoutProps<"/dashboard">) {
3 return (
4 <section className="grid grid-cols-[220px_1fr]">
5 <aside>Sidebar (state survives navigation)</aside>
6 <div>{children}</div>
7 </section>
8 );
9}
10
11// app/dashboard/loading.tsx - shown instantly while page.tsx streams
12export default function Loading() { // (app/dashboard/loading.tsx)
13 return <p>Loading dashboard…</p>;
14}
15
16// app/dashboard/page.tsx - async Server Component
17import { db } from "@/lib/db";
18
19export default async function DashboardPage() {
20 const stats = await db.stats.summary();
21 return <h1>{stats.activeUsers} active users</h1>;
22}

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the Next.js set.

Open in study app