Architecture (Feature-Sliced Design)
Priority: P2 (MEDIUM)
Adopt Feature-Sliced Design (FSD) for scalable applications.
Warning: FSD introduces boilerplate. Use it only if the project is expected to grow significantly (e.g., 20+ features). For smaller projects, a simple module-based structure is preferred.
Strategy
- RSC Boundaries: Enforce strict serialization rules for props passed from Server to Client. See RSC Boundaries & Serialization.
- App Layer is Thin: The
app/ directory (App Router) is 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: Do not preemptively create Entities.
- Rule: Start logic in
Features or Pages. Move to Entities only when data/logic is 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 the specific directory layout and layer definitions, see the 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 the 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-2
3description: "Scalable project structure using Feature-Sliced Design (FSD). Use when structuring a Next.js project with Feature-Sliced Design architecture. (triggers: src/features/**, src/entities/**, src/widgets/**, FSD, Feature Sliced Design, slices, segments)"
4metadata:
5 source: https://skillmd.com/skills/hoangnguyen0403/nextjs-architecture-2
6---
7
8# Architecture (Feature-Sliced Design)
9
10## **Priority: P2 (MEDIUM)**
11
12Adopt **Feature-Sliced Design (FSD)** for scalable applications.
13**Warning**: FSD introduces boilerplate. Use it only if the project is expected to grow significantly (e.g., 20+ features). For smaller projects, a simple module-based structure is preferred.
14
15## Strategy
16
171. **RSC Boundaries**: Enforce strict serialization rules for props passed from Server to Client. See [RSC Boundaries & Serialization](references/RSC_BOUNDARIES.md).
182. **App Layer is Thin**: The `app/` directory (App Router) is **only** for Routing.
19 - _Rule_: `page.tsx` should only import Widgets/Features. No business logic (`useEffect`, `fetch`) directly in pages.
203. **Slices over Types**: Group code by **Business Domain** (User, Product, Cart), not by File Type (Components, Hooks, Utils).
21 - _Bad_: `src/components/LoginForm.tsx`, `src/hooks/useLogin.ts`
22 - _Good_: `src/features/auth/login/` containing both.
234. **Layer Hierarchy**: Code can only import from _layers below it_.
24 - `App` -> `Widgets` -> `Features` -> `Entities` -> `Shared`.
255. **Avoid Excessive Entities**: Do not preemptively create Entities.
26 - _Rule_: Start logic in `Features` or `Pages`. Move to `Entities` **only** when data/logic is strictly reused across multiple differing features.
27 - _Rule_: Simple CRUD belongs in `shared/api`, not `entities`.
286. **Standard Segments**: Use standard segment names within slices.
29 - `ui` (Components), `model` (State/actions), `api` (Data fetching), `lib` (Helpers), `config` (Constants).
30 - _Avoid_: `components`, `hooks`, `services` as segment names.
31
32## Structure Reference
33
34For the specific directory layout and layer definitions, see the reference documentation.
35
36- [**FSD Folder Structure**](references/fsd-structure.md)
37- [**Bundling & Compatibility**](references/BUNDLING.md)
38- [**Runtime Selection (Edge/Node)**](references/RUNTIME_SELECTION.md)
39- [**Debug Tricks & MCP**](references/DEBUG_TRICKS.md)
40
41## Architecture Checklist (Mandatory)
42
43- [ ] **Layer Imports**: Does any layer import from a layer ABOVE it? (App > Widgets > Features > Entities > Shared)
44- [ ] **Page Logic**: Is `page.tsx` thin, containing only Widgets/Features and zero `useEffect`/`fetch`?
45- [ ] **RSC Boundaries**: Are Server Components isolated from Client Components with proper 'use client' boundaries?
46- [ ] **Public API**: Is all access to a slice performed via the top-level `index.ts` (public API)?
47- [ ] **Cross-Slice**: Do slices within the same layer (e.g., two features) import from each other directly? (Prohibited)
48
49- **Server Actions**: Place them in the `model/` folder of a Feature (e.g., `features/auth/model/actions.ts`).
50- **Data Access (DAL)**: Place logic in the `model/` folder of an Entity (e.g., `entities/user/model/dal.ts`).
51- **UI Components**: Base UI (shadcn) belongs in `shared/ui`. Feature-specific UI belongs in `features/*/ui`.
52
53
54## Anti-Patterns
55
56- **No cross-slice imports**: Slices in the same layer must not import from each other directly.
57- **No business logic in `page.tsx`**: Pages import Widgets/Features only; zero `useEffect`/`fetch`.
58- **No file-type folders**: Group by domain (`features/auth/`), not type (`components/`, `hooks/`).
59- **No premature Entity creation**: Start in Features; move to Entities only on strict reuse.
60