Skip to content

How to understand a Next.js (App Router) codebase

In an App Router project the folder tree under `app/` is the route map. Components are Server Components unless a file says `"use client"`, so the first job when reading is to work out which code runs on the server, which ships to the browser, and where the boundary sits.

How to tell it's Next.js (App Router)

  • An app/ directory (or src/app/) containing layout.tsx and page.tsx files
  • next in package.json dependencies and a next.config.ts or next.config.js
  • Files starting with "use client" or "use server" directives
  • Special files such as loading.tsx, error.tsx, not-found.tsx and route.ts

Where things usually live

app/layout.tsx
Root layout: the <html> shell, fonts, providers and anything shared by every page.
app/**/page.tsx
One page per route. The folder path is the URL; [slug] folders are dynamic segments.
app/**/route.ts
Route handlers: HTTP endpoints exporting GET, POST and so on.
app/(group)/
Route groups. Parentheses organise files and share layouts without changing the URL.
components/
Shared UI. Check each file for "use client" to see what runs in the browser.
lib/ or server/
Data access, auth helpers and server-only logic called from pages and actions.
proxy.ts (or middleware.ts)
Runs before routes match: redirects, rewrites and auth checks. Next.js 16 renamed middleware.ts to proxy.ts.
next.config.ts
Build and runtime settings: redirects, images, experimental flags such as Cache Components.

Conventions, not rules. Real projects rename and move things, so confirm against the repository.

Where to start reading

  1. app/layout.tsx, to see global providers and what wraps every page
  2. The page.tsx for the route you care about, then follow its imports
  3. proxy.ts or middleware.ts, because it can redirect a request before any page runs
  4. next.config.ts, for rewrites, redirects and flags that change behaviour

How a request moves through the code

  1. 1The request hits proxy.ts (or middleware.ts) if one exists, which may redirect, rewrite or add headers.
  2. 2Next.js matches the URL to a folder under app/ and renders the layouts from the root down to that segment.
  3. 3The page.tsx Server Component runs on the server, fetching data directly or through helpers in lib/.
  4. 4Client Components marked "use client" are rendered to HTML on the server, then hydrated in the browser.
  5. 5Form submissions and mutations call Server Actions ("use server" functions) or route.ts handlers.
  6. 6Cached data is reused until it is revalidated, so check "use cache", revalidatePath and revalidateTag calls when output looks stale.

Questions to ask about the repository

  • Which components in this app are Client Components, and why does each one need to be?
  • Trace what happens from submitting the sign-up form to the user record being saved.
  • Where is authentication enforced: in proxy, in layouts, in Server Actions or in route handlers?
  • Which routes are dynamic, and what data does each one fetch?
  • What does next.config.ts change about the default behaviour?

What trips people up

  • A layout does not re-render on navigation between its child pages, so auth checks placed only in a layout can be skipped by client-side navigation to sibling pages; check pages or data access too.
  • Importing a server-only module into a Client Component silently pulls it toward the browser bundle; look for the server-only package guarding sensitive modules.
  • Older tutorials describe getServerSideProps and pages/; those belong to the Pages Router and don't apply here.
  • Caching rules changed between major versions, so check the next version in package.json before trusting an explanation of why data is or isn't fresh.