Okay, let's break down a scalable folder structure for a modern Next.js project leveraging the App Router (which is the default and recommended approach in recent versions).
The core idea is to blend Next.js's conventions with patterns that promote modularity, maintainability, and separation of concerns, especially as the project grows.
Key Principles:
- App Router Conventions: Stick to Next.js's routing conventions within the
app/ directory.
src/ Directory: Encapsulate most of your application code within a src/ directory for a clean separation from configuration files.
- Feature-Based Organization: Group code related to specific features together. This is crucial for scalability.
- Shared Code: Isolate reusable components, hooks, utilities, etc., in dedicated top-level folders within
src/.
- Clear Boundaries: Differentiate between routing/layout definitions (
app/) and the UI components/logic implementing them.
Recommended Structure:
my-next-app/
├── .env.local # Environment variables
├── .eslintrc.json # ESLint configuration
├── .gitignore
├── next.config.js # Next.js configuration
├── package.json
├── postcss.config.js # PostCSS configuration (often for Tailwind)
├── README.md
├── tailwind.config.ts # Tailwind CSS configuration (if used)
├── tsconfig.json # TypeScript configuration
├── public/ # Static assets (images, fonts, etc.)
│ ├── favicon.ico
│ └── ...
├── src/ # Main application source code
│ ├── app/ # Next.js App Router directory
│ │ ├── (marketing)/ # Example: Route group for marketing pages (no URL segment)
│ │ │ ├── layout.tsx # Layout for marketing pages
│ │ │ ├── page.tsx # Marketing home page (/ route)
│ │ │ └── about/
│ │ │ ├── page.tsx # About page (/about route)
│ │ │ └── loading.tsx # Optional loading UI for /about
│ │ │
│ │ ├── (app)/ # Example: Route group for the main application section
│ │ │ ├── layout.tsx # Main app layout (e.g., with sidebar, header)
│ │ │ ├── dashboard/
│ │ │ │ ├── page.tsx # Dashboard home page (/dashboard route)
│ │ │ │ └── loading.tsx
│ │ │ ├── settings/
│ │ │ │ ├── page.tsx # Settings index page (/settings route)
│ │ │ │ └── profile/
│ │ │ │ ├── page.tsx # Profile page (/settings/profile route)
│ │ │ │ └── loading.tsx
│ │ │ │
│ │ │ └── ... # Other app sections
│ │ │
│ │ ├── api/ # API Routes
│ │ │ ├── auth/
│ │ │ │ └── route.ts # /api/auth endpoint
│ │ │ └── ...
│ │ │
│ │ ├── layout.tsx # Root layout (required)
│ │ ├── page.tsx # Root page (/)
│ │ ├── globals.css # Global styles
│ │ ├── loading.tsx # Optional global loading UI
│ │ └── error.tsx # Optional global error boundary UI
│ │
│ ├── components/ # Shared UI components (atoms, molecules, reusable UI elements)
│ │ ├── ui/ # Truly generic UI elements (Button, Input, Card, etc.)
│ │ │ ├── Button.tsx
│ │ │ ├── Input.tsx
│ │ │ └── ...
│ │ ├── layout/ # Layout-related components (Header, Footer, Sidebar, etc.)
│ │ │ ├── Header.tsx
│ │ │ ├── Footer.tsx
│ │ │ └── ...
│ │ └── ... # Other shared components
│ │
│ ├── features/ # Feature-based modules (domain-specific logic & UI)
│ │ ├── authentication/
│ │ │ ├── components/ # Components specific to authentication (LoginForm, SignupForm)
│ │ │ │ ├── LoginForm.tsx
│ │ │ │ └── ...
│ │ │ ├── hooks/ # Hooks specific to authentication (useAuth)
│ │ │ │ └── useAuth.ts
│ │ │ ├── services/ # API calls or logic for authentication
│ │ │ │ └── authAPI.ts
│ │ │ ├── context/ # Auth context if needed
│ │ │ │ └── AuthContext.tsx
│ │ │ └── types/ # Types specific to authentication
│ │ │ └── index.ts
│ │ │
│ │ ├── products/
│ │ │ ├── components/ # ProductCard, ProductList, etc.
│ │ │ ├── hooks/ # useProducts, useProduct
│ │ │ ├── services/ # productsAPI.ts
│ │ │ ├── utils/ # Product-specific utils
│ │ │ └── types/
│ │ │
│ │ └── ... # Other features (profile, orders, etc.)
│ │
│ ├── hooks/ # Shared custom hooks (generic, not tied to a specific feature)
│ │ ├── useDebounce.ts
│ │ ├── useLocalStorage.ts
│ │ └── ...
│ │
│ ├── lib/ # Utility functions, helpers, core logic, external configurations
│ │ ├── utils.ts # General utility functions
│ │ ├── api.ts # Base API client configuration (e.g., axios instance)
│ │ ├── constants.ts # Application-wide constants
│ │ ├── validation.ts # Validation schemas/functions
│ │ └── ...
│ │
│ ├── providers/ # Global context providers or state management wrappers
│ │ ├── QueryProvider.tsx # e.g., TanStack Query ClientProvider
│ │ ├── StateProvider.tsx # e.g., Redux Provider
│ │ └── ThemeProvider.tsx
│ │
│ ├── services/ # Shared services/API functions (if not feature-specific)
│ │ └── ... # e.g., analyticsService.ts
│ │
│ ├── store/ # Global state management (Redux, Zustand, Jotai)
│ │ ├── index.ts # Store configuration
│ │ ├── reducers/ # (If using Redux)
│ │ └── ...
│ │
│ ├── styles/ # Shared styles, theme variables, base CSS overrides
│ │ ├── theme.ts # Theme variables (colors, spacing)
│ │ └── base.css # Any base resets or styles not global
│ │
│ ├── types/ # Shared TypeScript types and interfaces
│ │ ├── index.ts
│ │ └── next-auth.d.ts # Example: Augmenting NextAuth types
│ │
│ └── context/ # Shared React Contexts (if not feature-specific)
│ └── ThemeContext.tsx
│
└── tests/ # Optional: Unit and integration tests (can also be co-located)
├── components/
├── features/
└── ...
Explanation:
app/: This directory is managed by Next.js for routing.
- Folders: Define URL segments (e.g.,
app/products/[id]/).
- Route Groups
(...): Use parentheses (marketing), (app) to group routes without affecting the URL path. This is great for applying different layouts to different sections of your app.
- Special Files:
page.js/page.tsx (defines UI for a route), layout.js/layout.tsx (defines shared UI for a route segment), loading.js/loading.tsx (loading UI), error.js/error.tsx (error UI), route.js/route.ts (API routes).
public/: For static files directly served by the Next.js server (favicon, robots.txt, images not processed by Next/Image).
src/: Contains almost everything else.
components/: Holds reusable, presentational UI components. Often divided into ui/ (generic elements like Button, Input) and layout/ (Header, Footer). These components generally don't fetch data directly or manage feature-specific logic.
features/: This is the core of scalability. Each subfolder represents a distinct feature or domain of your application (e.g., authentication, product management, user profile). Inside each feature folder, you can organize components, hooks, API calls, state, types, etc., specific only to that feature. This keeps related code together and minimizes dependencies between features.
hooks/: For custom React hooks that are generic and reused across multiple features.
lib/ (or utils/): For pure utility functions, helper modules, constants, base API configurations, validation logic, etc. Code here should ideally have no dependencies on React or Next.js features.
providers/: Central place for wrapping your application or route segments with necessary Context Providers (like Zustand, Redux, Theme, QueryClient).
services/ (or api/): Can be used for shared API logic or service configurations that aren't tied to a specific feature. Often, API logic is better placed feature folders ().
Client vs. Server Components:
- You don't necessarily need separate folders. The
use client directive at the top of a component file marks it as a Client Component.
- Generally, try to keep components as Server Components by default. Move logic or interactivity that requires hooks (
useState, useEffect) or event listeners into separate Client Components, importing them into your Server Components.
- Place Client Components within their respective feature folders or the shared
components/ folder as appropriate.
Flexibility:
This is a robust starting point. You might adapt it based on:
- Project Size: Smaller projects might not need the full feature breakdown initially.
- Team Preference: Agree on conventions with your team.
- Specific Needs: You might add folders like
contexts/, layouts/ (if separate from app/ structure), validators/, etc.
The key is the principle of colocation (keeping related code together, often within feature folders) and separation of concerns.