Project Structure & File Conventions
The app/ directory is not simply a folder where React components happen to live. It is an input to the Next.js router and build system.
The key mental model is:
Folders describe route segments; special file names activate framework behavior inside those segments. Ordinary files are just modules.
Start with the smallest route treeβ
app/
βββ layout.tsx
βββ page.tsx
app/layout.tsxis the root layout.app/page.tsxexposes/.
Add a route:
app/
βββ layout.tsx
βββ page.tsx
βββ products/
βββ page.tsx
Now app/products/page.tsx exposes /products.
The products folder defines a segment, but the folder alone is not a public page.
Top-level project filesβ
A production project may look like:
my-app/
βββ app/
βββ public/
βββ components/
βββ lib/
βββ instrumentation.ts
βββ instrumentation-client.ts
βββ proxy.ts
βββ next.config.ts
βββ package.json
βββ tsconfig.json
βββ eslint.config.mjs
βββ .env.local
βββ .gitignore
Not every project needs every file.
app/β
The modern App Router route tree. This handbook does not use pages/.
public/β
Static files served from the site root.
For example:
public/logo.svg
can be addressed as:
/logo.svg
Do not use public/ as a dumping ground for secrets or server-only files. Public means publicly retrievable.
next.config.tsβ
Framework configuration. Add options intentionally because configuration can affect compilation, routing, caching, assets, security headers, and deployment output.
instrumentation.tsβ
Server-side instrumentation entry point used for observability setup and related hooks.
instrumentation-client.tsβ
Client-side instrumentation that runs early in the browser, useful for monitoring, performance marks, and browser error setup.
Because this file runs in the browser, never place server secrets in it.
proxy.tsβ
The Next.js 16 request-boundary convention formerly named Middleware.
Proxy can inspect selected incoming requests and perform actions such as redirects, rewrites, and header/cookie handling before route rendering.
It is not a replacement for authorization inside protected Server Functions, Route Handlers, or data-access operations.
.env*β
Environment configuration. Local/private environment files should generally stay out of version control.
Variables exposed with NEXT_PUBLIC_ are not secrets.
Routing special filesβ
These file names have framework meaning inside app/:
| Convention | Role |
|---|---|
page.tsx | Page UI that exposes a route |
layout.tsx | Shared layout that preserves across applicable navigation |
template.tsx | Layout-like boundary that receives a new instance on navigation |
loading.tsx | Loading UI / Suspense boundary for a segment |
error.tsx | Error boundary UI for a segment |
global-error.tsx | Global error fallback |
not-found.tsx | Not-found UI |
default.tsx | Fallback for parallel-route slots |
route.ts | Route Handler / HTTP endpoint |
Later chapters cover each behavior deeply. At foundation level, remember that the file name is part of the API.
Component hierarchy is generated from the route treeβ
For a nested segment, the effective component structure is conceptually similar to:
parent layout
βββ child layout
βββ template (if present)
βββ loading/error boundaries where applicable
βββ page or deeper child layout
The exact React/runtime implementation is not something application code should depend on. The public contract is the documented behavior of layouts, templates, loading, error, not-found, pages, and slots.
Colocation is safeβ
Suppose you have:
app/
βββ products/
βββ page.tsx
βββ ProductCard.tsx
βββ product.queries.ts
βββ product.types.ts
Only page.tsx is the page convention. The other files do not automatically become URLs.
This makes route-local colocation possible.
A common mistake from older routing systems is assuming every file beneath the route folder is public. That is not the App Router model.
Private foldersβ
Prefix a folder with _ when you want to make its non-routing intent explicit:
app/
βββ dashboard/
βββ _components/
β βββ RevenueChart.tsx
βββ page.tsx
Private folders are useful for organization and for preventing the directory from being considered by the routing system.
They are not a security boundary. A private folder name does not protect data or code from an attacker; it only affects route organization/build interpretation.
Route groupsβ
Parentheses create an organizational group without adding a URL segment:
app/
βββ (marketing)/
β βββ about/
β β βββ page.tsx
β βββ pricing/
β βββ page.tsx
βββ (app)/
βββ dashboard/
βββ page.tsx
The URLs remain:
/about
/pricing
/dashboard
not:
/(marketing)/about
Route groups let architecture and URL design evolve somewhat independently. They are especially useful for applying different layouts to logical sections.
Dynamic segmentsβ
Dynamic folders use brackets:
app/products/[slug]/page.tsx
This represents routes such as:
/products/keyboard
/products/monitor
Later routing chapters will cover:
[slug][...slug][[...slug]]- typed route params
- async params in modern Next.js
- static generation choices
- validation and not-found behavior
Do not read a dynamic URL segment as trusted database input. It is user-controlled input and must be validated before being used for sensitive operations.
Parallel and intercepting routesβ
The App Router also has advanced folder conventions for:
- parallel slots such as
@modal; - intercepting routes using conventions such as
(.)and related forms.
These are powerful for dashboards, modal routes, and independent route regions. They are deliberately deferred until the routing phase because using them without understanding layout persistence and client navigation usually creates confusing architecture.
Metadata conventionsβ
Next.js recognizes metadata files and APIs for concerns such as:
- icons;
- Open Graph images;
- Twitter images;
robots.txt;sitemap.xml;- manifest data.
The lesson is the same: a file name may be an application module and a framework convention. Always check what contract the convention activates.
A production-friendly organizationβ
There is no mandatory components/, lib/, features/, or services/ folder naming scheme in Next.js.
A reasonable medium-sized application might use:
app/
βββ (marketing)/
βββ (product)/
βββ api-or-route-handler-segments/
βββ layout.tsx
features/
βββ billing/
βββ projects/
βββ users/
components/
βββ ui/
lib/
βββ auth/
βββ db/
βββ observability/
The important architectural questions are:
- Can a developer find a feature's behavior?
- Are route concerns separated from domain logic when that improves clarity?
- Are dependencies flowing in a deliberate direction?
- Are server-only modules prevented from leaking into client graphs?
- Can authorization and tenant scoping be audited?
Folder aesthetics are secondary.
src/ is optionalβ
Next.js supports moving application source into src/:
src/
βββ app/
while keeping project configuration at the repository root.
Use it if the team prefers that separation. It does not make an application more βenterprise.β
Root layout mental modelβ
A root layout might be:
import type { Metadata } from 'next'
import './globals.css'
export const metadata: Metadata = {
title: 'Acme',
description: 'Acme application',
}
export default function RootLayout({
children,
}: Readonly<{ children: React.ReactNode }>) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
It is not a replacement for the old concept of βone component that manually renders every page.β It participates in a nested route-layout tree managed by Next.js.
Layouts are Server Components by default unless a client boundary is introduced.
page.tsx mental modelβ
A page is the UI leaf for a public route:
export default async function ProductsPage() {
return (
<main>
<h1>Products</h1>
</main>
)
}
The fact that this function can be async is important in App Router architecture: a Server Component can await server-side work directly.
That does not mean every page should perform every query. Data ownership and architecture still matter.
route.ts is not a pageβ
A Route Handler exposes HTTP behavior:
app/status/route.ts
and a page exposes UI:
app/status/page.tsx
Do not casually mix both conventions at the same route segment without checking the framework's routing constraints. Design whether a path is an HTTP resource or a rendered page intentionally.
Framework magic checklistβ
Whenever a file βdoes something automatically,β ask:
- Which file/folder convention triggered it?
- Does it alter the URL tree?
- Does it create a layout/boundary or expose a route?
- Does the module execute on the server, client, or both through different phases?
- Does it change the client bundle?
- Does it affect rendering/caching?
- Is the behavior core Next.js or deployment-provider behavior?
Common mistakesβ
Making every component a Client Componentβ
Adding 'use client' high in the tree pulls an entire import boundary into the client graph. Keep interactive boundaries deliberate.
Treating private folders as authorizationβ
_admin does not protect /admin. Folder structure is not access control.
Encoding organization into URLs accidentallyβ
Use route groups when a directory exists for team/layout organization but should not become a URL segment.
Over-engineering folders before features existβ
Start with clear route ownership and cohesive feature boundaries. Extract shared layers when actual coupling appears.
Copying middleware.ts into a Next.js 16 greenfield appβ
Use the current proxy.ts convention. Treat Middleware terminology as migration/history.
Exerciseβ
Design a route tree for:
/
/pricing
/login
/dashboard
/dashboard/projects
/dashboard/projects/[projectId]
/settings/profile
/settings/billing
Requirements:
- marketing pages share one layout;
- authenticated application pages share another layout;
- route-group names must not appear in URLs;
- project-local UI should be colocated;
[projectId]must be treated as untrusted input;- do not add
'use client'unless a component actually needs browser/interactivity features.
Then draw the resulting layout nesting before writing code.
Interview questionsβ
Does every folder inside app/ become a route?
No. Folders form route segments, but public UI/endpoints are exposed through documented conventions such as page.tsx or route.ts.
What is a route group?
An organizational folder wrapped in parentheses whose name is omitted from the URL. It can help partition layouts and route organization.
Why use a private folder if colocated files are already safe?
For explicit organization, avoiding routing consideration/name conflicts, and signaling implementation detailβnot for security.