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.4license: MIT5---6# Architecture (Feature-Sliced Design)78## **Priority: P2 (MEDIUM)**910**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.1112## Workflow: Create New Feature Slice13141. **Create feature folder** — `src/features/auth/login/` with `ui/`, `model/`, `api/` segments.152. **Add public API** — Export via `src/features/auth/login/index.ts`.163. **Wire into page** — Import feature widget in `app/login/page.tsx` (thin page).174. **Verify imports** — Ensure no upward or cross-slice imports violate layer hierarchy.1819## Layer Hierarchy2021`App (app/) -> Widgets -> Features -> Entities -> Shared`2223See [implementation examples](references/implementation.md) for thin page example.2425## Strategy26271. **RSC Boundaries**: Enforce strict serialization rules for props passed from Server to Client. See [RSC Boundaries & Serialization](references/RSC_BOUNDARIES.md).282. **App Layer Thin**: `app/` directory (App Router) **only** for Routing.29 - _Rule_: `page.tsx` should only import Widgets/Features. No business logic (`useEffect`, `fetch`) directly in pages.303. **Slices over Types**: Group code by **Business Domain** (User, Product, Cart), not by File Type (Components, Hooks, Utils).31 - _Bad_: `src/components/LoginForm.tsx`, `src/hooks/useLogin.ts`32 - _Good_: `src/features/auth/login/` containing both.334. **Layer Hierarchy**: Code can only import from _layers below it_.34 - `App` -> `Widgets` -> `Features` -> `Entities` -> `Shared`.355. **Avoid Excessive Entities**: not preemptively create Entities.36 - _Rule_: Start logic in `Features` or `Pages`. Move to `Entities` **only** when data/logic strictly reused across multiple differing features.37 - _Rule_: Simple CRUD belongs in `shared/api`, not `entities`.386. **Standard Segments**: Use standard segment names within slices.39 - `ui` (Components), `model` (State/actions), `api` (Data fetching), `lib` (Helpers), `config` (Constants).40 - _Avoid_: `components`, `hooks`, `services` as segment names.4142## Structure Reference4344For specific directory layout and layer definitions, see reference documentation.4546- [**FSD Folder Structure**](references/fsd-structure.md)47- [**Bundling & Compatibility**](references/BUNDLING.md)48- [**Runtime Selection (Edge/Node)**](references/RUNTIME_SELECTION.md)49- [**Debug Tricks & MCP**](references/DEBUG_TRICKS.md)5051## Architecture Checklist (Mandatory)5253- [ ] **Layer Imports**: any layer import from layer ABOVE it? (App > Widgets > Features > Entities > Shared)54- [ ] **Page Logic**: `page.tsx` thin, containing only Widgets/Features and zero `useEffect`/`fetch`?55- [ ] **RSC Boundaries**: Server Components isolated from Client Components with proper 'use client' boundaries?56- [ ] **Public API**: all access to slice performed via top-level `index.ts` (public API)?57- [ ] **Cross-Slice**: slices within same layer (e.g., two features) import from each other directly? (Prohibited)5859- **Server Actions**: Place them in `model/` folder of Feature (e.g., `features/auth/model/actions.ts`).60- **Data Access (DAL)**: Place logic in `model/` folder of Entity (e.g., `entities/user/model/dal.ts`).61- **UI Components**: Base UI (shadcn) belongs in `shared/ui`. Feature-specific UI belongs in `features/*/ui`.626364## Anti-Patterns6566- **No cross-slice imports**: Slices in same layer must not import from each other directly.67- **No business logic in `page.tsx`**: Pages import Widgets/Features only; zero `useEffect`/`fetch`.68- **No file-type folders**: Group by domain (`features/auth/`), not type (`components/`, `hooks/`).69- **No premature Entity creation**: Start in Features; move to Entities only on strict reuse.