Home
NextJS
NextJS - Project Structure
September 01, 2023
1 min

Table Of Contents

01
Folder and file conventions
02
Routes
03
Component Hierarchy
04
Project Organization Strategies

Folder and file conventions

Top-Level Folders

Top-level folders help organize your application code and static assets.

FolderPurpose
appApp Router (modern routing system)
pagesPages Router (legacy routing)
publicStatic assets (images, fonts, etc.)
srcOptional folder for application source code

The src folder is optional but recommended for separating application logic from configuration files.

Top-Level Files

These files configure and manage your project:

FilePurpose
next.config.jsNext.js configuration
package.jsonDependencies and scripts
instrumentation.tsOpenTelemetry and instrumentation
proxy.tsRequest proxy
.env*Environment variables (not tracked in Git)
eslint.config.mjsESLint configuration
tsconfig.jsonTypeScript configuration
jsconfig.jsonJavaScript configuration
next-env.d.tsType declarations (not tracked)
.gitignoreGit ignore rules

Routing File Conventions

Next.js uses file-based routing. Special files define behavior within each route segment.

FilePurpose
layout.tsxShared layout (header, nav, footer)
page.tsxPublic route page
loading.tsxLoading UI (Suspense boundary)
error.tsxError boundary
global-error.tsxGlobal error UI
not-found.tsx404 page
route.tsAPI endpoint
template.tsxRe-rendered layout
default.tsxParallel route fallback

Metadata File Conventions

App Icons

FilePurpose
favicon.icoBrowser favicon
icon.pngApp icon
apple-icon.pngApple devices

Open Graph & Social Images

FilePurpose
opengraph-image.pngFacebook/LinkedIn preview
twitter-image.pngTwitter preview

SEO Files

FilePurpose
sitemap.xmlSitemap
robots.txtRobots instructions

These can also be dynamically generated using .js or .ts.

Routes

Nested Routes

Folders define URL segments. Nested folders create nested routes.

Example:

app/
├── layout.tsx
├── page.tsx
└── blog/
├── layout.tsx
├── page.tsx
└── authors/
└── page.tsx

Resulting URLs:

  • /
  • /blog
  • /blog/authors

A route becomes publicly accessible only when a page.tsx or route.ts file exists.

Dynamic Routes

Dynamic routes use square brackets:

PatternExample URL
[slug]/blog/my-first-post
[...slug]/shop/clothing/shirts
[[...slug]]Optional catch-all

Example:

app/blog/[slug]/page.tsx

Access parameters via:

export default function Page({ params }) {
return <div>{params.slug}</div>
}

Route Groups

Wrap folder names in parentheses to organize routes without affecting the URL.

app/(marketing)/page.tsx → /

Useful for:

  • Grouping marketing pages
  • Sharing layouts
  • Creating multiple root layouts

Private Folders

Prefix with underscore to exclude from routing:

app/blog/_components/Post.tsx
app/blog/_lib/data.ts

Useful for:

  • UI utilities
  • Helper functions
  • Avoiding naming conflicts

Component Hierarchy

Special files render in this order:

layout
template
error
loading
not-found
page (or nested layout)

Each nested route inherits and wraps with parent layouts recursively.

example
example

Project Organization Strategies

Next.js is flexible. Choose what fits your team.

We separate the app into 4 layers:

1️⃣ Interface Layer → Next.js routing (app/)
2️⃣ Domain Layer → features/ (business logic)
3️⃣ Shared Layer → reusable UI / hooks / utils
4️⃣ Infrastructure → API, config, env, services
my-app/
├── public/ # Static assets
├── src/
│ ├── app/ # 🚨 ROUTING ONLY (Next.js layer)
│ │ ├── (public)/ # Marketing pages
│ │ │ ├── page.tsx
│ │ │ └── layout.tsx
│ │ │
│ │ ├── (auth)/ # Auth experience
│ │ │ ├── login/page.tsx
│ │ │ └── layout.tsx
│ │ │
│ │ ├── (dashboard)/ # Protected app
│ │ │ ├── products/page.tsx
│ │ │ ├── orders/page.tsx
│ │ │ └── layout.tsx
│ │ │
│ │ ├── api/ # BFF layer (thin controllers)
│ │ │ └── products/route.ts
│ │ │
│ │ ├── layout.tsx # Root layout
│ │ └── globals.css
│ │
│ ├── features/ # 🧠 DOMAIN-DRIVEN MODULES
│ │ ├── auth/
│ │ │ ├── api/
│ │ │ ├── hooks/
│ │ │ ├── components/
│ │ │ ├── schemas/
│ │ │ └── types.ts
│ │ │
│ │ ├── products/
│ │ │ ├── api/
│ │ │ │ ├── getProducts.ts
│ │ │ │ └── createProduct.ts
│ │ │ │
│ │ │ ├── components/
│ │ │ │ ├── ProductCard.tsx
│ │ │ │ └── ProductTable.tsx
│ │ │ │
│ │ │ ├── hooks/
│ │ │ │ └── useProducts.ts
│ │ │ │
│ │ │ ├── services/ # Business rules
│ │ │ │ └── product.service.ts
│ │ │ │
│ │ │ ├── schemas/
│ │ │ │ └── product.schema.ts
│ │ │ │
│ │ │ └── types.ts
│ │ │
│ │ └── orders/
│ │
│ ├── components/ # 🔁 Shared reusable UI
│ │ ├── ui/
│ │ │ ├── Button.tsx
│ │ │ ├── Modal.tsx
│ │ │ └── Input.tsx
│ │ │
│ │ ├── form/
│ │ └── layout/
│ │
│ ├── hooks/ # Global hooks
│ │ ├── useDebounce.ts
│ │ ├── useMediaQuery.ts
│ │ └── useAuth.ts
│ │
│ ├── lib/ # ⚙️ Infrastructure Layer
│ │ ├── fetcher.ts # API client
│ │ ├── env.ts # Typed env config
│ │ ├── logger.ts
│ │ ├── analytics.ts
│ │ └── auth.ts
│ │
│ ├── providers/ # React providers
│ │ ├── QueryProvider.tsx
│ │ ├── ThemeProvider.tsx
│ │ └── SessionProvider.tsx
│ │
│ ├── styles/
│ │ └── tokens.css
│ │
│ ├── types/ # Global types
│ │ └── global.d.ts
│ │
│ └── config/ # App-level configs
│ ├── routes.ts
│ └── navigation.ts
├── tests/ # E2E + integration tests
│ ├── e2e/
│ └── integration/
├── .env
├── next.config.js
├── tsconfig.json
├── package.json
└── README.md

Tags

#NextJS

Share

Related Posts

NextJS
NextJS - Data Fetching
September 06, 2023
1 min
© 2026, All Rights Reserved.
Powered By

Social Media

githublinkedinyoutube