All skills
Skillintermediate

App Router Architecture

``` app/ ├── layout.tsx # Root layout (required) ├── page.tsx # Home page (/) ├── loading.tsx # Loading UI ├── error.tsx # Error boundary ├── not-found.tsx # 404 page ├── template.tsx # Re-mounted layout │ ├── (marketing)/ # Route group (no URL segment) │ ├── layout.tsx │ ├── about/ │ │ └── page.tsx # /about │ └── contact/ │ └── page.tsx # /contact │ ├── dashboard/ │ ├── layout.tsx # Shared dashbo

Claude Code Knowledge Pack7/10/2026

Overview

App Router Architecture

File-Based Routing

app/
├── layout.tsx              # Root layout (required)
├── page.tsx               # Home page (/)
├── loading.tsx            # Loading UI
├── error.tsx              # Error boundary
├── not-found.tsx          # 404 page
├── template.tsx           # Re-mounted layout
│
├── (marketing)/           # Route group (no URL segment)
│   ├── layout.tsx
│   ├── about/
│   │   └── page.tsx      # /about
│   └── contact/
│       └── page.tsx      # /contact
│
├── dashboard/
│   ├── layout.tsx        # Shared dashboard layout
│   ├── page.tsx          # /dashboard
│   ├── settings/
│   │   └── page.tsx      # /dashboard/settings
│   └── @analytics/       # Parallel route (slot)
│       └── page.tsx
│
├── blog/
│   ├── [slug]/
│   │   └── page.tsx      # /blog/my-post (dynamic)
│   └── [...slug]/
│       └── page.tsx      # /blog/a/b/c (catch-all)
│
└── api/
    └── users/
        └── route.ts      # API route handler

Root Layout (Required)

// app/layout.tsx

const inter = Inter({ subsets: ['latin'] })

  title: {
    default: 'My App',
    template: '%s | My App'
  },
  description: 'Next.js 14 application',
}

  children,
}: {
  children: React.ReactNode
}) {
  return (
    <html lang="en">
      <body className={inter.className}>
        {children}
      </body>
    </html>
  )
}

Nested Layouts

// app/dashboard/layout.tsx

  children,
}: {
  children: React.ReactNode
}) {
  const session = await auth()

  if (!session) {
    redirect('/login')
  }

  return (
    <div className="flex">
      
      <main className="flex-1">{children}</main>
    </div>
  )
}

Templates (Re-mount on Navigation)

// app/template.tsx
'use client'

  useEffect(() => {
    // Runs on every navigation
    console.log('Template mounted')
  }, [])

  return <div>{children}</div>
}

Loading States

// app/dashboard/loading.tsx

  return (
    <div className="flex items-center justify-center h-screen">
      <div className="animate-spin rounded-full h-32 w-32 border-b-2" />
    </div>
  )
}

Error Boundaries

// app/error.tsx
'use client'

  error,
  reset,
}: {
  error: Error & { digest?: string }
  reset: () => void
}) {
  return (
    <div>
      <h2>Something went wrong!</h2>
      <button onClick={() => reset()}>Try again</button>
    </div>
  )
}

Route Groups

// (marketing) and (shop) share the same URL level
app/
├── (marketing)/
│   ├── layout.tsx      # Marketing layout
│   └── about/
│       └── page.tsx    # /about
└── (shop)/
    ├── layout.tsx      # Shop layout
    └── products/
        └── page.tsx    # /products

Parallel Routes

// app/dashboard/layout.tsx

  children,
  analytics,
  team,
}: {
  children: React.ReactNode
  analytics: React.ReactNode
  team: React.ReactNode
}) {
  return (
    <>
      {children}
      {analytics}
      {team}
    </>
  )
}

// app/dashboard/@analytics/page.tsx

  return <div>Analytics Dashboard</div>
}

Intercepting Routes

// Show modal when navigating from same app
// but show full page on direct navigation

// app/photos/[id]/page.tsx (full page)

  return <div>Photo {params.id} - Full Page</div>
}

// app/@modal/(.)photos/[id]/page.tsx (modal)

  return <div>Photo {params.id} - Modal</div>
}

Dynamic Routes

// app/blog/[slug]/page.tsx

  return <h1>Post: {params.slug}</h1>
}

// Generate static params at build time

  const posts = await fetch('https://api.example.com/posts').then(res => res.json())

  return posts.map((post: { slug: string }) => ({
    slug: post.slug,
  }))
}

// Opt out of static generation

// Revalidate every 60 seconds

Catch-All Routes

// app/docs/[...slug]/page.tsx
// Matches: /docs/a, /docs/a/b, /docs/a/b/c

  return <div>Docs: {params.slug.join('/')}</div>
}

// Optional catch-all: [[...slug]]
// Also matches: /docs

Route Handlers (API Routes)

// app/api/users/route.ts

  const users = await db.user.findMany()
  return NextResponse.json(users)
}

  const body = await request.json()
  const user = await db.user.create({ data: body })
  return NextResponse.json(user, { status: 201 })
}

// Dynamic routes: app/api/users/[id]/route.ts

  request: NextRequest,
  { params }: { params: { id: string } }
) {
  const user = await db.user.findUnique({ where: { id: params.id } })
  return NextResponse.json(user)
}

Metadata API

// app/blog/[slug]/page.tsx

  { params }: { params: { slug: string } }
): Promise {
  const post = await fetchPost(params.slug)

  return {
    title: post.title,
    description: post.excerpt,
    openGraph: {
      title: post.title,
      description: post.excerpt,
      images: [{ url: post.coverImage }],
    },
  }
}

Quick Reference

FilePurposeUse Case
layout.tsxPersistent UI across routesShared navigation, auth wrapper
page.tsxRoute UIActual page content
loading.tsxLoading fallbackAutomatic Suspense boundary
error.tsxError boundaryHandle errors gracefully
template.tsxRe-mounted layoutAnalytics, animations
not-found.tsx404 pageCustom not found UI
route.tsAPI handlerBackend API endpoints