50 Mins

2. The App Router & Project Structure

Core Concept

The App Router & Project Structure

In the last module you created a file called page.tsx and it just... became a webpage. No import, no route config, no registration step. That probably felt a little like magic. This module takes the magic apart.

The App Router works on one simple idea: your folder structure is your URL structure. Instead of writing a routing config file that maps paths to components, you express that mapping by how you organize files inside the app/ directory. Once this idea clicks, the rest of Next.js becomes far more predictable — because almost everything else in this course is really just "a special file that Next.js recognizes inside that same folder structure."

Project Anatomy

Here's what a freshly created Next.js project looks like. Don't worry about memorizing all of it — focus on app/, since that"s where you"ll spend nearly all your time in this course:

Bash
my-nextjs-app/
├── app/
│   ├── layout.tsx          # Root layout for the entire app
│   ├── page.tsx            # Homepage
│   ├── about/
│   │   └── page.tsx        # /about page
│   ├── blog/
│   │   ├── layout.tsx      # Blog section layout
│   │   ├── page.tsx        # /blog page
│   │   └── [slug]/
│   │       └── page.tsx    # /blog/[slug] dynamic page
│   └── api/
│       └── products/
│           └── route.ts    # API endpoint: /api/products
├── public/
│   ├── images/
│   │   └── logo.png
│   └── favicon.ico
├── components/
│   ├── Header.tsx
│   └── Footer.tsx
├── lib/
│   └── utils.ts
├── next.config.js
├── package.json
└── tsconfig.json

Two folders outside app/ are worth noting: public/ holds static files served as-is (images, fonts, favicons — reachable directly at /logo.png), and components/ is just a convention, not a Next.js requirement, for reusable pieces shared across routes.

Creating Routes: Folders and Files

Here's the rule that drives everything: a folder inside app/ defines a route segment, but it only becomes a visitable page once you add a page.tsx file inside it. A folder with no page.tsx is not reachable by URL — it exists only to organize other routes or hold shared files.

So app/about/page.tsx maps to /about. app/blog/page.tsx maps to /blog. There's no configuration step in between — the file's location is the configuration.

React (TSX)
// app/about/page.tsx
export default function AboutPage() {
  return (
    <div>
      <h1>About Us</h1>
      <p>This page lives at the /about URL automatically.</p>
    </div>
  );
}
Output Visiting /about renders "About Us" — no route registration needed anywhere else in the project.

Dynamic segments — parts of the URL that change, like a blog post's ID — are written by wrapping the folder name in square brackets: [slug]. Next.js treats that as a variable and hands you its value through a params prop in your page component. You can also match multiple segments at once with [...slug] (catch-all) or [[...slug]] (optional catch-all) — we'll put these to real use in Module 5.

Special Files and Their Roles

Next.js App Router: Component Boundary Hierarchy How Next.js automatically wraps and nests route segments into a unified component hierarchy layout.tsx (Persistent Shell - does not re-render on navigation) template.tsx (Creates new instance per navigation) error.tsx (Client Error Boundary - catches child exceptions) loading.tsx (React Suspense fallback) page.tsx (Unique Route Leaf Component & Content)

Beyond page.tsx, the App Router recognizes a handful of other filenames as special. Each one slots into a specific part of the route's behavior automatically, just by existing in the right folder:

  • layout.tsx — shared UI that wraps this route and all its children, and persists (doesn't re-render) across navigation.
  • page.tsx — the unique, visitable content of a route.
  • loading.tsx — an automatic fallback UI shown while this route's content is being fetched on the server.
  • error.tsx — a React Error Boundary that catches errors in this segment and shows a fallback instead of crashing the whole app.
  • not-found.tsx — a custom 404 view for this route segment.
  • template.tsx — like a layout, but re-mounts fresh on every navigation, useful for UI that should reset its state each time.

You'll notice a pattern: none of these require an import or a manual hookup anywhere. You create the file, name it correctly, and Next.js wires it in. We'll build real loading.tsx and error.tsx files hands-on in Module 14.

Quick Check: Only page.tsx and route.ts actually make a segment reachable by URL. The rest — layouts, loading states, error boundaries — enhance a route but don't create one by themselves.
React (TSX)
// app/blog/error.tsx
'use client';
export default function ErrorBoundary({ error, reset }) {
  return (
    <div>
      <h2>Something went wrong!</h2>
      <button onClick={() => reset()}>Try again</button>
    </div>
  );
}

The Root Layout

Every Next.js App Router project requires exactly one layout.tsx at the root of app/ — the Root Layout. It's the only layout allowed to define the <html> and <body> tags, because it wraps literally every page your app will ever render.

React (TSX)
// app/layout.tsx
export default function RootLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    <html lang='en'>
      <body>
        {children}
      </body>
    </html>
  );
}
Output Every page in the app is wrapped with this HTML shell, including any global styles or fonts imported here.

Notice the children prop — that's where whatever page you're currently visiting gets slotted in. The Root Layout always runs on the server and can't use hooks directly; if part of it needs interactivity, that piece becomes its own Client Component, imported and placed inside the layout.

Nested Routes and Layouts

Layouts aren't limited to the root. Drop a layout.tsx into any folder inside app/, and it automatically wraps every page in that folder and its subfolders — no import needed on the page's side.

This is how you get a persistent sidebar on a dashboard, or a consistent header across every blog post, without repeating that markup in every single page file. Layouts nest automatically: the Root Layout wraps every section layout, which wraps the individual page.

React (TSX)
// app/blog/layout.tsx
export default function BlogLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    <div className='blog-wrapper'>
      <nav>Blog Navigation</nav>
      <main>{children}</main>
    </div>
  );
}
Output Every route under /blog — including /blog and /blog/[slug] — now renders inside this shared nav and main wrapper.

One important side effect: because a layout persists across navigation between its own child routes, its component state is preserved. Navigating from one blog post to another won't reset scroll position or re-run the layout's effects — only the page.tsx content underneath actually swaps out.

Route Groups: Organizing Without Affecting URLs

Sometimes you want to group routes for organizational reasons — say, all your marketing pages together, separate from your dashboard pages — without those groupings showing up in the URL. Wrapping a folder name in parentheses, like (marketing), does exactly that: it's excluded from the URL path entirely.

Bash
app/
├── (marketing)/
│   ├── layout.tsx      # Layout only for marketing pages
│   ├── page.tsx        # Maps to /  (not /marketing)
│   └── pricing/
│       └── page.tsx    # Maps to /pricing (not /marketing/pricing)
└── (dashboard)/
    ├── layout.tsx      # A completely different layout
    └── settings/
        └── page.tsx    # Maps to /settings

This is especially useful for giving two sections of the same app entirely different Root-adjacent layouts, without polluting the URL structure to express that split. We'll use route groups again when we look at multi-layout dashboards later in the course.

Where You're Headed

You now understand the rule the entire App Router is built on: folders define routes, special filenames define behavior, and layouts nest automatically. In the next module, we'll dig into what actually happens when one of these routes renders — starting with the concept that makes Next.js fundamentally different from plain React: Server Components.

Deep Dive Reference: App Router Architecture

File Conventions Dictionary

Next.js strictly enforces file names. If you misspell them, they are ignored. Here is the rigorous reference:

FilenamePurposeExtension Support
page.tsxDefines the unique UI of a route. Makes the route publicly accessible..js, .jsx, .ts, .tsx
layout.tsxShared UI that wraps a segment and its children. Does NOT re-render on navigation..js, .jsx, .ts, .tsx
template.tsxLike a layout, but creates a new instance (re-mounts) on every navigation. Useful for enter/exit animations..js, .jsx, .ts, .tsx
loading.tsxFallback UI for the segment (auto-wraps the page in a React Suspense boundary)..js, .jsx, .ts, .tsx
error.tsxError boundary for the segment. Must be a Client Component..js, .jsx, .ts, .tsx
route.tsServer-side API endpoint. Cannot exist in the same folder as a page.tsx..js, .ts

Knowledge Check

Ready to test your understanding of 2. The App Router & Project Structure?

Back