Architecture (Feature-Sliced Design)
Priority: P2 (MEDIUM)
Warning: FSD introduces boilerplate. Use it only if project expected to grow significantly (e.g., 20+ features). For smaller projects, simple module-based structure preferred.
Workflow: Create New Feature Slice
- Create feature folder —
src/features/auth/login/ with ui/, model/, api/ segments.
- Add public API — Export via
src/features/auth/login/index.ts.
- Wire into page — Import feature widget in
app/login/page.tsx (thin page).
- Verify imports — Ensure no upward or cross-slice imports violate layer hierarchy.
Layer Hierarchy
App (app/) -> Widgets -> Features -> Entities -> Shared
See implementation examples for thin page example.
Strategy
- RSC Boundaries: Enforce strict serialization rules for props passed from Server to Client. See RSC Boundaries & Serialization.
- App Layer Thin:
app/ directory (App Router) only for Routing.
- Rule:
page.tsx should only import Widgets/Features. No business logic (useEffect, fetch) directly in pages.
- Slices over Types: Group code by Business Domain (User, Product, Cart), not by File Type (Components, Hooks, Utils).
- Bad:
src/components/LoginForm.tsx, src/hooks/useLogin.ts
- Good:
src/features/auth/login/ containing both.
- Layer Hierarchy: Code can only import from layers below it.
App -> Widgets -> Features -> Entities -> Shared.
- Avoid Excessive Entities: not preemptively create Entities.
- Rule: Start logic in
Features or Pages. Move to Entities only when data/logic strictly reused across multiple differing features.
- Rule: Simple CRUD belongs in
shared/api, not entities.
- Standard Segments: Use standard segment names within slices.
ui (Components), model (State/actions), api (Data fetching), lib (Helpers), config (Constants).
- Avoid:
components, hooks, services as segment names.
Structure Reference
For specific directory layout and layer definitions, see reference documentation.
- FSD Folder Structure
- Bundling & Compatibility
- Runtime Selection (Edge/Node)
- Debug Tricks & MCP
Architecture Checklist (Mandatory)
Anti-Patterns
- No cross-slice imports: Slices in same layer must not import from each other directly.
- No business logic in
page.tsx: Pages import Widgets/Features only; zero useEffect/fetch.
- No file-type folders: Group by domain (
features/auth/), not type (components/, hooks/).
- No premature Entity creation: Start in Features; move to Entities only on strict reuse.
1---
2name: nextjs-architecture
3description: Structure Next.js projects with Feature-Sliced Design layers, domain-grouped slices, and strict import hierarchy. Use when organizing features into FSD layers, enforcing slice boundaries, or keeping page.tsx thin.
4metadata:
5 source: https://skillmd.com/skills/hoangnguyen0403/nextjs-architecture
6 triggers:
7 files:
8 - 'src/features/**'
9 - 'src/entities/**'
10 - 'src/widgets/**'
11 keywords:
12 - FSD
13 - Feature Sliced Design
14 - slices
15 - segments
16---
17
18# Architecture (Feature-Sliced Design)
19
20## **Priority: P2 (MEDIUM)**
21
22**Warning**: FSD introduces boilerplate. Use it only if project expected to grow significantly (e.g., 20+ features). For smaller projects, simple module-based structure preferred.
23
24## Workflow: Create New Feature Slice
25
261. **Create feature folder** — `src/features/auth/login/` with `ui/`, `model/`, `api/` segments.
272. **Add public API** — Export via `src/features/auth/login/index.ts`.
283. **Wire into page** — Import feature widget in `app/login/page.tsx` (thin page).
294. **Verify imports** — Ensure no upward or cross-slice imports violate layer hierarchy.
30
31## Layer Hierarchy
32
33`App (app/) -> Widgets -> Features -> Entities -> Shared`
34
35See [implementation examples](references/implementation.md) for thin page example.
36
37## Strategy
38
391. **RSC Boundaries**: Enforce strict serialization rules for props passed from Server to Client. See [RSC Boundaries & Serialization](references/RSC_BOUNDARIES.md).
402. **App Layer Thin**: `app/` directory (App Router) **only** for Routing.
41 - _Rule_: `page.tsx` should only import Widgets/Features. No business logic (`useEffect`, `fetch`) directly in pages.
423. **Slices over Types**: Group code by **Business Domain** (User, Product, Cart), not by File Type (Components, Hooks, Utils).
43 - _Bad_: `src/components/LoginForm.tsx`, `src/hooks/useLogin.ts`
44 - _Good_: `src/features/auth/login/` containing both.
454. **Layer Hierarchy**: Code can only import from _layers below it_.
46 - `App` -> `Widgets` -> `Features` -> `Entities` -> `Shared`.
475. **Avoid Excessive Entities**: not preemptively create Entities.
48 - _Rule_: Start logic in `Features` or `Pages`. Move to `Entities` **only** when data/logic strictly reused across multiple differing features.
49 - _Rule_: Simple CRUD belongs in `shared/api`, not `entities`.
506. **Standard Segments**: Use standard segment names within slices.
51 - `ui` (Components), `model` (State/actions), `api` (Data fetching), `lib` (Helpers), `config` (Constants).
52 - _Avoid_: `components`, `hooks`, `services` as segment names.
53
54## Structure Reference
55
56For specific directory layout and layer definitions, see reference documentation.
57
58- [**FSD Folder Structure**](references/fsd-structure.md)
59- [**Bundling & Compatibility**](references/BUNDLING.md)
60- [**Runtime Selection (Edge/Node)**](references/RUNTIME_SELECTION.md)
61- [**Debug Tricks & MCP**](references/DEBUG_TRICKS.md)
62
63## Architecture Checklist (Mandatory)
64
65- [ ] **Layer Imports**: any layer import from layer ABOVE it? (App > Widgets > Features > Entities > Shared)
66- [ ] **Page Logic**: `page.tsx` thin, containing only Widgets/Features and zero `useEffect`/`fetch`?
67- [ ] **RSC Boundaries**: Server Components isolated from Client Components with proper 'use client' boundaries?
68- [ ] **Public API**: all access to slice performed via top-level `index.ts` (public API)?
69- [ ] **Cross-Slice**: slices within same layer (e.g., two features) import from each other directly? (Prohibited)
70
71- **Server Actions**: Place them in `model/` folder of Feature (e.g., `features/auth/model/actions.ts`).
72- **Data Access (DAL)**: Place logic in `model/` folder of Entity (e.g., `entities/user/model/dal.ts`).
73- **UI Components**: Base UI (shadcn) belongs in `shared/ui`. Feature-specific UI belongs in `features/*/ui`.
74
75
76## Anti-Patterns
77
78- **No cross-slice imports**: Slices in same layer must not import from each other directly.
79- **No business logic in `page.tsx`**: Pages import Widgets/Features only; zero `useEffect`/`fetch`.
80- **No file-type folders**: Group by domain (`features/auth/`), not type (`components/`, `hooks/`).
81- **No premature Entity creation**: Start in Features; move to Entities only on strict reuse.