feature-sliced-design
npx skills add https://github.com/unerue/skills --skill feature-sliced-design
Agent 安装分布
Skill 文档
Feature-Sliced Design Architecture
Overview
Provides guidance for implementing Feature-Sliced Design (FSD v2.1) in Next.js applications. FSD organizes code into a layered hierarchy that prevents circular dependencies and promotes maintainability. This skill uses a custom views layer instead of standard pages â page business logic lives in /src/views, routing in /src/app/.
Reference files (load as needed):
- layers-and-segments.md â Layer definitions, segment patterns, @x cross-reference, migration details
- examples.md â Full code examples for each layer (app, views, widgets, features, entities, shared)
- monorepo.md â (Optional) Turborepo + FSD structure for monorepo projects
Purpose
Feature-Sliced Design (FSD) is an architectural methodology for organizing frontend applications into a standardized, scalable structure. It provides clear separation of concerns through a layered hierarchy that prevents circular dependencies and promotes maintainability.
Why use FSD
- Scalability: Grows naturally as your application expands
- Maintainability: Clear boundaries make refactoring safer
- Team collaboration: Consistent structure enables parallel development
- Onboarding: New developers understand architecture quickly
Unified src/app/ structure
Both Next.js App Router and FSD App layer live in src/app/. Next.js recognizes layout.tsx, page.tsx, etc. inside src/app/, so all code stays under src/ without a root-level app/ folder, aligning with FSD v2.1.
Custom ‘views’ layer
This skill uses ‘views’ instead of the standard FSD ‘pages’ layer. Page business logic goes in /src/views; routing structure stays in /src/app/.
When to Use
Apply Feature-Sliced Design when:
- Starting new Next.js projects that require clear architectural boundaries
- Refactoring growing codebases that lack consistent structure
- Working with multi-developer teams needing standardized organization
- Building applications with complex business logic requiring separation of concerns
- Scaling applications where circular dependencies become problematic
- Creating enterprise applications with long-term maintenance requirements
- (Optional) Developing monorepo applications (Turborepo, etc.) â see monorepo.md
Core Principles (FSD v2.1)
Layer Hierarchy
FSD v2.1 organizes code into 7 layers (from most to least responsibility/dependency):
- app – App-wide matters (entrypoint, providers, global styles, router config)
- processes – Deprecated; move contents to
featuresandapp - views – Page-level business logic (custom naming; standard FSD uses ‘pages’)
- widgets – Large self-sufficient UI blocks
- features – Main user interactions and business value
- entities – Business domain objects and models
- shared – Foundation (UI kit, API client, libs, config)
You don’t have to use every layer â add only what brings value. Most projects use at least Shared, Pages (or views), and App.
Import rule: A module can only import from layers strictly below it.
âââââââââââââââââââ
â app â â Can import from all layers below
âââââââââââââââââââ¤
â views â â Can import: widgets, features, entities, shared
âââââââââââââââââââ¤
â widgets â â Can import: features, entities, shared
âââââââââââââââââââ¤
â features â â Can import: entities, shared
âââââââââââââââââââ¤
â entities â â Can import: shared only
âââââââââââââââââââ¤
â shared â â Cannot import from any FSD layer
âââââââââââââââââââ
‘Views’ vs ‘Pages’ Layer
src/app/: Next.js routing + FSD App layer âlayout.tsx,page.tsx, route groups, providers, styles.page.tsximports from@/viewsand renders.src/views/: Page business logic, component composition â View components, models, API calls. Composes widgets, features, entities.
Slices and Public API
Slices are domain-based partitions within layers (except app and shared). Examples: views/dashboard, widgets/header, features/auth, entities/user.
Each slice exports through index.ts. Use explicit exports â avoid export * from. Consumers import from public API only; avoid deep imports like @/features/auth/ui/LoginForm.
Segments
Purpose-based groupings within slices:
- ui/ – React components, visual elements
- model/ – Business logic, state management, TypeScript types
- api/ – API clients, data fetching, external integrations
- lib/ – One area of focus per library; document in README
- config/ – Configuration constants, feature flags
Shared layer segments: api, ui, lib, config, i18n, routes
Naming Conventions
| Target | Convention | Example |
|---|---|---|
| Folders, slices | kebab-case | user-profile/, theme-toggle/ |
| Component files | kebab-case | login-form.tsx, user-card.tsx |
| Hook files | camelCase | useAuth.ts, useDashboard.ts |
| Store files | camelCase | authStore.ts |
| Function/API collections | camelCase | userApi.ts, formatDate.ts |
FSD with Next.js App Router
Next.js recognizes routing files inside src/app/. All FSD layers live under src/; no root-level app/ folder.
File organization:
my-nextjs-app/
âââ src/
â âââ app/ # Next.js routing + FSD App layer
â â âââ layout.tsx
â â âââ page.tsx
â â âââ dashboard/page.tsx
â â âââ providers/
â â âââ styles/
â âââ views/
â âââ widgets/
â âââ features/
â âââ entities/
â âââ shared/
â âââ ui/
â âââ lib/
â âââ api/
âââ package.json
Routing pages import from views:
// src/app/dashboard/page.tsx
import { DashboardView } from '@/views/dashboard';
export default function DashboardPage() {
return <DashboardView />;
}
(Optional) Monorepo: See monorepo.md
For full directory structure and layer examples, see references.
Workflow
Step 1: Set Up Layer Directories
mkdir -p src/{app,views,widgets,features,entities,shared}
mkdir -p src/app/{providers,styles,config}
mkdir -p src/shared/{ui,lib,api,config}
Step 2: Create First Entity
Start with entities (bottom layer). Define core business models in entities/{name}/model/ and API in entities/{name}/api/. Export via index.ts.
Step 3: Build Features Using Entities
Create features in features/{name}/. Features import from entities and shared only.
Step 4: Compose Widgets from Features
Build composite widgets in widgets/{name}/. Widgets can import from features, entities, shared.
Step 5: Assemble Views
Create page-level views in views/{name}/. Views compose widgets, features, entities.
Step 6: Connect to App Router
Wire views to Next.js routing. page.tsx imports from @/views/{slice}.
Import Rules
Allowed
- Layer importing from layer below
- Any layer importing from shared
- Slice importing different slice in lower layer
Forbidden
- Layer importing from same or higher layer
- Cross-slice imports within same layer
- Shared importing from FSD layers
Fixing Circular Dependencies
- Extract shared logic to lower layer (entities or shared)
- Create higher layer (widget) that imports both
- Review if slice should be split
Public API Enforcement
Always use index.ts to control exports. Import from public API only:
// â
Correct
import { LoginForm } from '@/features/auth';
// â Wrong (deep import)
import { LoginForm } from '@/features/auth/ui/LoginForm';
Migration Strategy (Bottom-up)
- Start with shared layer â extract UI, lib, API client
- Define entities â business domain objects
- Extract features â user interactions
- Build widgets â composite UI blocks
- Organize views â move page logic from page files
- Configure app layer â providers, styles
Migrate incrementally; run tests after each layer.
Best Practices
- No cross-slice imports within same layer
- Export through
index.ts; avoid deep imports - Colocate tests next to implementation
- Keep slices focused; avoid “god slices”
- Name by domain:
features/product-searchnotfeatures/search-bar-component - Use TypeScript strict mode
Troubleshooting
Import path issues: Configure path aliases in tsconfig.json â see Configuration below.
Build errors: Clear .next, run npm install, restart dev server.
Configuration
TypeScript Path Aliases
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/app/*": ["src/app/*"],
"@/views/*": ["src/views/*"],
"@/widgets/*": ["src/widgets/*"],
"@/features/*": ["src/features/*"],
"@/entities/*": ["src/entities/*"],
"@/shared/*": ["src/shared/*"]
}
},
"include": ["src"]
}
ESLint (Optional)
'no-restricted-imports': ['error', {
patterns: [{
group: ['@/views/*', '@/widgets/*'],
message: 'Features cannot import from views or widgets',
}],
}]
Reference Files
Documentation Library
Load these resources as needed during development:
Core FSD Reference
- layers-and-segments.md â Layer definitions (app, views, widgets, features, entities, shared), segment patterns (ui, model, api, lib, config), @x cross-reference, valid/invalid import examples, migration details
Implementation Examples
- examples.md â Full code examples for:
- Standalone Next.js structure
- Each layer (app providers, views, widgets, features, entities, shared)
- Segment patterns (model/store, lib/validation, form handling, server components)
Optional
- monorepo.md â Turborepo + FSD structure for monorepo projects
External