Project Structure, Colocation, Route Groups & Vertical Slices
Next.js intentionally does not prescribe one application folder architecture.
That freedom is useful, but large repositories need a consistent local rule.
The official App Router project-structure guidance establishes several important facts:
- folders under
appdefine route segments; - a route does not become publicly routable until a
pageorroutefile exists; - implementation files can therefore be safely colocated in route segments;
- private folders opt a subtree out of routing;
- route groups organise routes without changing the URL;
srcis optional.
Use those primitives to express product ownership instead of fighting them.
1. Three common organisation strategiesβ
Strategy A β route-first colocationβ
app/
dashboard/
projects/
page.tsx
_components/
_queries/
_actions/
Good when a feature exists only for one route subtree.
Strategy B β feature-first modules outside appβ
app/
dashboard/projects/page.tsx
features/
projects/
ui/
server/
commands/
dto/
index.ts
Good when a capability spans many routes or entry points.
Strategy C β hybridβ
app/
(app)/
projects/
page.tsx
_components/
features/
projects/
server/
domain/
commands/
Route-specific UI stays close to the route, while durable business ownership lives in the feature module.
For large applications, the hybrid is often practical.
2. Keep the route tree readableβ
The app tree should primarily communicate:
URL structure
layout structure
loading/error boundaries
public HTTP endpoints
route ownership
If the route tree is dominated by hundreds of generic utility modules, navigation becomes harder.
Private folders can make internal implementation explicit:
app/
dashboard/
projects/
_components/
_lib/
page.tsx
The underscore is an organisational signal, not a security boundary.
3. Route groups are architecture toolsβ
Route groups such as:
(marketing)
(app)
(admin)
can organise routes by:
product surface
team
layout requirement
authentication context
shell
Because the group name is not part of the URL, it can reflect internal ownership.
Example:
app/
(marketing)/
pricing/page.tsx
blog/page.tsx
(app)/
dashboard/page.tsx
projects/page.tsx
(admin)/
admin/users/page.tsx
But do not create conflicting paths in multiple groups.
4. Multiple root layouts are deployment-like boundariesβ
Different root layouts can be useful for genuinely separate application surfaces.
However, navigation between different root layouts performs a full page load.
That means the boundary affects user experience and should not be introduced only for folder aesthetics.
Ask:
Do these surfaces need independent document shells?
Do they share navigation state?
Are users expected to move between them frequently?
5. Route ownership should be obviousβ
A route directory should make it clear which capability owns it.
Good:
app/(app)/billing/
app/(app)/projects/
app/(app)/reports/
Less clear:
app/(app)/misc/
app/(app)/common/
The same rule applies outside app.
6. Vertical slice structureβ
A feature module can contain all layers needed for one capability:
features/projects/
domain/
policy.ts
types.ts
server/
dal.ts
queries.ts
commands/
create-project.ts
archive-project.ts
ui/
project-card.tsx
rename-project-form.tsx
dto/
project.dto.ts
telemetry/
events.ts
index.ts
This does not mean every feature must have every folder.
Create structure only when the feature needs it.
7. Avoid architecture-by-templateβ
A common failure mode:
every feature
repositories/
services/
managers/
factories/
use-cases/
presenters/
before any real complexity exists.
Architecture should remove ambiguity, not manufacture ceremony.
Start with the minimum stable boundary and evolve when pressure appears.
8. Use feature entry pointsβ
Instead of cross-feature deep imports:
import { buildInvoiceRows } from '@/features/billing/internal/table/buildRows'
prefer:
import { getInvoiceSummary } from '@/features/billing'
The feature's index.ts or package exports becomes its supported public surface.
Internal files stay changeable.
9. Route-specific modules should stay localβ
If a component is used by exactly one route, placing it in a global components/ folder makes ownership less clear.
Prefer:
app/(app)/projects/[id]/_components/project-header.tsx
Promote it only when another owner genuinely needs it.
10. Global component folders should be narrowβ
Reasonable global UI categories:
components/ui/ β design-system primitives
components/layout/ β cross-product shell
components/providers/β truly global providers
Avoid putting feature-specific components there.
11. Keep server code visibly server-ownedβ
Use naming/folder signals plus server-only for privileged modules where appropriate.
features/projects/server/dal.ts
features/projects/server/queries.ts
import 'server-only'
This reduces accidental imports from Client Components.
12. Keep browser-only implementation explicitβ
Likewise, browser-dependent modules can live under an obvious client boundary:
features/editor/client/
or behind a Client Component entry point.
Do not make server-owned modules import browser utilities.
13. Separate primitives from product componentsβ
A design-system button:
packages/ui/button.tsx
should not know about:
billing plan
project permission
analytics business event
router path
A product-specific component can compose the primitive:
export function UpgradePlanButton() {
return <Button>Upgrade</Button>
}
This keeps the design system reusable and stable.
14. Organise by change couplingβ
Files that usually change together should often live near each other.
If adding a project field always requires touching:
project validator
project DTO
project form
project command
putting those modules under one capability makes the change easier to understand.
15. Avoid a global utils graveyardβ
utils tends to mix unrelated abstractions.
Prefer meaningful owners:
lib/date/
lib/http/
lib/ids/
features/projects/slug.ts
features/billing/money.ts
A generic helper should be generic in semantics, not merely small in size.
16. Define import directionβ
Example policy:
app β features β core/infrastructure
features β shared primitives
features β other feature internals
ui primitives β product features
Use tooling to enforce it when repository scale warrants it.
17. Avoid accidental client dependency expansionβ
A Client Component entry point can pull imported modules into the client graph.
If a feature barrel exports both server and client modules indiscriminately:
export * from './server/dal'
export * from './ui/client-widget'
client imports can become confusing or invalid.
Prefer environment-specific entry points:
features/projects/server
features/projects/client
features/projects/types
or carefully separated exports.
18. Types can still leak architectureβ
A shared TypeScript type that directly exposes a database entity can couple every layer to the database schema.
Prefer deliberate contracts:
export type ProjectSummary = {
id: string
name: string
canEdit: boolean
}
rather than exporting the complete ORM record everywhere.
19. Project structure should support deletionβ
A strong feature boundary should be removable with a bounded change.
Ask:
If we delete Reports, how many unrelated folders must we inspect?
If the answer is βthe whole repository,β the feature is too entangled.
20. Example large-app structureβ
src/
app/
(marketing)/
(app)/
dashboard/
projects/
billing/
api/
features/
identity/
projects/
billing/
reporting/
core/
config/
db/
observability/
queue/
components/
ui/
shell/
This is an example, not a Next.js requirement.
The correct structure is the one that makes ownership and dependency direction obvious for your product.
21. Senior review questionsβ
Should all code live under app?β
No. Next.js supports colocation there, but durable product modules can live outside the route tree when that makes ownership clearer.
Why use route groups if they do not affect the URL?β
They let the repository express internal product/team/layout organisation independently from public URL design.
Are private folders required for non-routable code?β
No. Colocated files are not automatically routes. Private folders are an explicit organisational convention that opts their subtree out of routing.
Production checklistβ
- route tree communicates URL and layout structure clearly
- route-specific code stays local where useful
- feature-wide business code has a durable owner
- private folders and route groups are used intentionally
- multiple root layouts justify full-page navigation cost
- deep cross-feature imports are avoided
- server/client entry points are separated
- generic folders have narrow definitions
- module deletion has a bounded blast radius
- import-direction rules are documented/enforced
Exerciseβ
Refactor a hypothetical SaaS repository currently organised as:
components/
services/
hooks/
utils/
into a hybrid route + vertical-slice architecture for:
- identity
- projects
- billing
- reporting
Explain why each file belongs to its owner.