#1 Data Analytics Program in India
₹2,499₹1,499Enroll Now
12 min read
•Question 29 of 47hard

Next.js App Router Architecture Deep Dive

Understanding the internal architecture of App Router.

App Router Architecture

The App Router represents a fundamental shift in how Next.js handles routing, rendering, and data fetching.

React Server Components Foundation

code.txtTSX
// Server Component (default)
async function ServerComponent() {
  // Runs only on server - can use:
  // - Direct database access
  // - File system operations
  // - Secrets/environment variables
  const data = await db.query('SELECT * FROM users');
  return <UserList users={data} />;
}

// Client Component
'use client';
function ClientComponent() {
  const [state, setState] = useState();
  // Runs on client - can use:
  // - Hooks (useState, useEffect)
  // - Browser APIs
  // - Event handlers
}

Component Tree Architecture

code.txtTSX
// Layout hierarchy
app/
ā”œā”€ā”€ layout.tsx      // Root layout (wraps all)
ā”œā”€ā”€ page.tsx        // Home page
ā”œā”€ā”€ dashboard/
│   ā”œā”€ā”€ layout.tsx  // Dashboard layout (nested)
│   ā”œā”€ā”€ page.tsx    // /dashboard
│   └── settings/
│       └── page.tsx // /dashboard/settings

Request Lifecycle

  1. URL Parsing: Match route segments
  2. Layout Resolution: Build layout hierarchy
  3. Data Fetching: Parallel fetch at segment level
  4. Rendering: Server → Client hydration
  5. Streaming: Progressive UI delivery

Advanced Patterns

code.txtTSX
// Nested layouts with parallel data fetching
export default async function DashboardLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  // This fetch runs in parallel with page fetches
  const user = await getUser();

  return (
    <div>
      <Sidebar user={user} />
      <main>{children}</main>
    </div>
  );
}