Introduction: Why Your Folder Structure Matters
Remember when you first started coding and thought, "I'll just put all my files in one folder, I'll remember what everything does!" Oh, sweet summer child. Three months later, you're scrolling through 87 files named final.js
, finalFINAL.js
, and the infamous PLEASE_WORK.js
.
Good folder structure in Next.js isn't just about satisfying your inner organizing goddess – it directly impacts your SEO, development speed, and your team's will to live when they need to find that one component at 11 PM before a deadline.
The Foundation: Root-Level Organization
Let's start with the basics. A well-structured Next.js project looks something like this:
my-nextjs-project/
├── app/ # App Router
├── public/ # Static assets
├── src/ # Application source code
├── .env # Environment variables
├── .eslintrc.js # ESLint configuration
├── next.config.js # Next.js configuration
├── package.json # Project dependencies
└── tsconfig.json # TypeScript configuration
"But wait," I hear you ask, "where does my actual code go?"
Hold tight! We're about to dive into the src
folder – the true kingdom where your code will either thrive in harmonious order or descend into file chaos that would make Marie Kondo weep uncontrollably.
The Mighty src Directory: Your New Best Friend
Placing your code inside a src
directory isn't mandatory, but it's like using a napkin at a barbecue restaurant – technically optional but highly recommended unless you enjoy sticky fingers and confused glances.
Here's what your src
folder should look like:
src/
├── app/ # App Router
│ ├── layout.tsx # Root layout
│ ├── page.tsx # Homepage
│ └── (routes)/ # Grouped routes
├── components/ # Shared components
│ ├── ui/ # UI components
│ └── features/ # Feature-specific components
├── lib/ # Utility functions
├── hooks/ # Custom React hooks
├── styles/ # Global styles
├── types/ # TypeScript type definitions
└── context/ # React Context providers
The App Router: Secret Weapon from Next js 13+
The app
directory is where the magic happens. This is where the App Router configuration lives:
app/
├── layout.tsx # Root layout (required)
├── page.tsx # Homepage
├── about/ # /about route
│ └── page.tsx # About page
├── blog/ # /blog route
│ ├── page.tsx # Blog index page
│ └── [slug]/ # Dynamic blog post route
│ └── page.tsx # Individual blog post page
├── (marketing)/ # Route group (doesn't affect URL)
│ ├── features/ # /features route
│ │ └── page.tsx
│ └── pricing/ # /pricing route
│ └── page.tsx
└── api/ # API endpoints
└── hello/
└── route.ts # GET/POST handlers
Think of the app
directory as a map of your website. Each folder represents a route, and the page.tsx
file is what gets rendered. It's so intuitive that even your product manager might understand it (no promises, though).
Components: Lego Blocks for Adults
Your components folder is like your sock drawer – if organized well, it makes life easier; if not, you're constantly digging through a mess just to find a matching pair.
Here's a structure that won't make future you curse present you:
components/
├── ui/ # Reusable UI components
│ ├── Button/
│ │ ├── index.tsx
│ │ ├── Button.test.tsx
│ │ └── Button.module.css
│ ├── Card/
│ └── Input/
├── layout/ # Layout components
│ ├── Header/
│ ├── Footer/
│ └── Sidebar/
└── features/ # Feature-specific components
├── auth/ # Authentication-related components
│ ├── LoginForm/
│ └── SignupForm/
└── blog/ # Blog-related components
├── PostCard/
└── CommentSection/
I once worked on a project where all components were in a single folder. Finding anything was like trying to locate a specific grain of rice in a bowl of... well, rice. Don't be that developer.
The Rest of the Gang: Supporting Directories
Now let's talk about the other folders
that keep your codebase from turning into spaghetti:
lib/ # Utility functions
├── api/ # API client functions
├── helpers/ # Helper functions
└── constants/ # Constant values
hooks/ # Custom React hooks
├── useForm.ts
├── useLocalStorage.ts
└── useMediaQuery.ts
styles/ # Global styles
├── globals.css
└── variables.css
types/ # TypeScript type definitions
├── api.types.ts
└── common.types.ts
These directories are like those kitchen drawers that hold all the miscellaneous stuff – important, but not quite deserving their own room.
SEO Optimization Through Structure
Here's where your folder structure directly impacts SEO:
Predictable URLs: With the App Router, your folder structure directly maps to your URLs, making them logical and predictable
Faster Build Times: Organized code means faster builds and better
performance, which search engines loveEfficient Metadata Management: Keep your metadata files close to the pages they describe
For example, place your metadata files directly in the route folders:
app/
├── about/
│ ├── page.tsx
│ └── metadata.ts # About page metadata
└── blog/
├── page.tsx
└── metadata.ts # Blog index metadata
Real-World Example: The Blog Platform
Let's look at a real-world example for a blog platform:
src/
├── app/
│ ├── layout.tsx # Main layout with header/footer
│ ├── page.tsx # Homepage
│ ├── blog/ # Blog section
│ │ ├── page.tsx # Blog index
│ │ ├── categories/ # /blog/categories route
│ │ │ └── [category]/ # Dynamic category pages
│ │ │ └── page.tsx
│ │ └── [slug]/ # Individual blog posts
│ │ └── page.tsx
│ └── about/ # About page
│ └── page.tsx
├── components/
│ ├── ui/ # Reusable UI components
│ │ ├── Button/
│ │ └── Card/
│ └── features/
│ └── blog/
│ ├── PostCard/
│ └── CategoryList/
├── lib/
│ ├── api/
│ │ └── getBlogPosts.ts # Fetch blog posts
│ └── mdx/
│ └── markdownToHtml.ts # Convert markdown to HTML
└── types/
└── blog.types.ts # Blog-related types
This structure makes it immediately obvious where to find blog-related code and how the routes map to the website structure.
Common Pitfalls and How to Avoid Them
The "Everything in Pages" Syndrome: In older Next.js projects, developers stuffed everything into the pages directory. With the App Router, resist the urge to put all your code in the app directory.
Component File Explosion: Having 200+ files in your components folder is like having 200+ items on a restaurant menu – nobody knows what's good anymore. Use subdirectories!
The "Utils" Black Hole: Having a single utils.ts file that grows to 2,000 lines is a cry for help. Break utility functions into logical groups in the lib directory.
Nesting Too Deep: If your file path looks like
src/components/features/dashboard/widgets/weather/current/small/index.tsx
, you've gone too far. Nobody wants to traverse seven directories to find a component.
Don't forget to throw a like on this post if it saved you from folder structure issues.....
Top comments (1)
Great explanations, there are so many different ways to set up a project's folder structure.