Back to Guides
Framework Combos

Encode Your Next.js Folder Structure for AI Agents

Your folder structure is the most important convention to encode. Once an AI knows where things go, everything else follows.

Last updated: 2026-07-01

Folder structure as the foundation

The folder structure is the single most impactful thing to encode in your rules file. It determines where every new file goes -and getting this right means your AI never creates files in wrong locations. More importantly, a well-defined structure implies the architecture: where business logic lives, what files are server-only, and what the boundary between UI and data is.

Three structure tiers for Next.js

Small projects (< 10 routes): flat structure with shared components. Medium projects: add features/ or domains/ with colocated components and hooks. Large projects: full feature-based architecture with src/features/[domain]/ modules, each self-contained with its own components, hooks, actions, queries, and types. The right structure for your project depends on team size and expected growth.

Medium project structure template

The most commonly applicable structure -feature-grouped but not fully domain-modular.

CLAUDE.md -Folder Structure section
# Folder Structure

```
src/
├── app/                    # Routes ONLY. Thin pages that import from below.
│   ├── (marketing)/        # Route group -shared marketing layout
│   ├── (dashboard)/        # Route group -shared dashboard layout
│   └── api/                # Route handlers: route.ts
│
├── components/             # Shared UI used in 2+ routes
│   ├── ui/                 # Base UI primitives (Button, Input, Badge)
│   └── [feature]-card.tsx  # Composed components
│
├── features/               # Business domain modules
│   └── [feature]/
│       ├── components/     # Feature-specific UI
│       ├── hooks/          # Feature-specific client hooks
│       ├── actions/        # Server Actions for this feature
│       ├── queries/        # Data fetching functions (server-side)
│       ├── types.ts        # Feature-specific types
│       └── index.ts        # Public API -only import from this
│
├── hooks/                  # Shared custom hooks (used in 2+ features)
├── lib/                    # Server-only: Prisma, auth, Stripe
│   └── (all files must import 'server-only')
├── store/                  # Zustand stores (UI state only)
├── types/                  # Global TypeScript types and Zod schemas
└── utils/                  # Pure helpers (usable on client and server)
```

## Placement Rules
- New component for one feature → src/features/[feature]/components/
- New component for 2+ features → src/components/
- New server utility → src/lib/  (add import 'server-only')
- New type/interface → src/features/[feature]/types.ts or src/types/ if global
- New Zustand store → src/store/[domain]-store.ts

## Never Create
- pages/ directory (App Router project)
- src/helpers/ or src/misc/ (use utils/ or lib/ instead)
- Top-level src/ folders not listed above

Generate folder structure rules for your project

standarx generates a folder structure rules file with your exact paths and conventions -for CLAUDE.md, .cursor/rules, and AGENTS.md.

Generate my rules file