Code Reference

App Router

Next.js · Reference cheat sheet

App Router

Next.js · Reference cheat sheet


📋 Overview

The App Router uses the app/ directory for routes, layouts, loading UI, and nested segments. Folders define URL segments; special files define UI and behavior.

🔧 Core concepts

FileRole
page.tsxUI for a route (required for that URL)
layout.tsxShared UI wrapping children; persists across navigations
loading.tsxInstant loading UI (Suspense boundary)
error.tsxError boundary for the segment
not-found.tsx404 UI
route.tsRoute Handler (API endpoint)
template.tsxLike layout but remounts on navigation
default.tsxFallback for parallel routes
ConventionMeaning
[id]Dynamic segment
[...slug]Catch-all
[[...slug]]Optional catch-all
(group)Route group — no URL segment
@slotParallel route slot

💡 Examples

Nested layout:

app/
  layout.tsx
  page.tsx          → /
  blog/
    layout.tsx
    page.tsx        → /blog
    [slug]/
      page.tsx      → /blog/:slug

Dynamic page:

// app/blog/[slug]/page.tsx
export default async function Post({
  params,
}: {
  params: Promise<{ slug: string }>;
}) {
  const { slug } = await params;
  return <h1>{slug}</h1>;
}

Route group (no URL impact):

app/(marketing)/about/page.tsx  → /about
app/(shop)/cart/page.tsx        → /cart

⚠️ Pitfalls

  • layout.tsx does not remount on soft navigation — don't put one-shot client state there expecting reset.
  • params and searchParams are async in recent Next.js — await them.
  • A folder without page.tsx is not a reachable route by itself.

On this page