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-architecture3description: 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.4---5# Architecture (Feature-Sliced Design)
6
7## **Priority: P2 (MEDIUM)**
8
9**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.
10
11## Workflow: Create New Feature Slice
12
131. **Create feature folder** — `src/features/auth/login/` with `ui/`, `model/`, `api/` segments.
142. **Add public API** — Export via `src/features/auth/login/index.ts`.
153. **Wire into page** — Import feature widget in `app/login/page.tsx` (thin page).
164. **Verify imports** — Ensure no upward or cross-slice imports violate layer hierarchy.
17
18## Layer Hierarchy
19
20`App (app/) -> Widgets -> Features -> Entities -> Shared`
21
22See [implementation examples](references/implementation.md) for thin page example.
23
24## Strategy
25
261. **RSC Boundaries**: Enforce strict serialization rules for props passed from Server to Client. See [RSC Boundaries & Serialization](references/RSC_BOUNDARIES.md).
272. **App Layer Thin**: `app/` directory (App Router) **only** for Routing.
28 - _Rule_: `page.tsx` should only import Widgets/Features. No business logic (`useEffect`, `fetch`) directly in pages.
293. **Slices over Types**: Group code by **Business Domain** (User, Product, Cart), not by File Type (Components, Hooks, Utils).
30 - _Bad_: `src/components/LoginForm.tsx`, `src/hooks/useLogin.ts`
31 - _Good_: `src/features/auth/login/` containing both.
324. **Layer Hierarchy**: Code can only import from _layers below it_.
33 - `App` -> `Widgets` -> `Features` -> `Entities` -> `Shared`.
345. **Avoid Excessive Entities**: not preemptively create Entities.
35 - _Rule_: Start logic in `Features` or `Pages`. Move to `Entities` **only** when data/logic strictly reused across multiple differing features.
36 - _Rule_: Simple CRUD belongs in `shared/api`, not `entities`.
376. **Standard Segments**: Use standard segment names within slices.
38 - `ui` (Components), `model` (State/actions), `api` (Data fetching), `lib` (Helpers), `config` (Constants).
39 - _Avoid_: `components`, `hooks`, `services` as segment names.
40
41## Structure Reference
42
43For specific directory layout and layer definitions, see reference documentation.
44
45- [**FSD Folder Structure**](references/fsd-structure.md)
46- [**Bundling & Compatibility**](references/BUNDLING.md)
47- [**Runtime Selection (Edge/Node)**](references/RUNTIME_SELECTION.md)
48- [**Debug Tricks & MCP**](references/DEBUG_TRICKS.md)
49
50## Architecture Checklist (Mandatory)
51
52- [ ] **Layer Imports**: any layer import from layer ABOVE it? (App > Widgets > Features > Entities > Shared)
53- [ ] **Page Logic**: `page.tsx` thin, containing only Widgets/Features and zero `useEffect`/`fetch`?
54- [ ] **RSC Boundaries**: Server Components isolated from Client Components with proper 'use client' boundaries?
55- [ ] **Public API**: all access to slice performed via top-level `index.ts` (public API)?
56- [ ] **Cross-Slice**: slices within same layer (e.g., two features) import from each other directly? (Prohibited)
57
58- **Server Actions**: Place them in `model/` folder of Feature (e.g., `features/auth/model/actions.ts`).
59- **Data Access (DAL)**: Place logic in `model/` folder of Entity (e.g., `entities/user/model/dal.ts`).
60- **UI Components**: Base UI (shadcn) belongs in `shared/ui`. Feature-specific UI belongs in `features/*/ui`.
61
62
63## Anti-Patterns
64
65- **No cross-slice imports**: Slices in same layer must not import from each other directly.
66- **No business logic in `page.tsx`**: Pages import Widgets/Features only; zero `useEffect`/`fetch`.
67- **No file-type folders**: Group by domain (`features/auth/`), not type (`components/`, `hooks/`).
68- **No premature Entity creation**: Start in Features; move to Entities only on strict reuse.