frontend-architecture
它能做什么
这项技能告诉你的 AI 如何拆分前端代码,让每个文件只负责一件事。涵盖 React、Next 和 Vue 项目。适合希望代码随项目增长而保持整洁的开发者。
适用场景
- ✓当你开始一个新的 React、Next 或 Vue 项目,需要一个清晰结构的时候。
- ✓当你的前端文件把显示、数据获取和校验混在一起的时候。
- ✓当某个组件文件变得太大,你想按职责拆分它的时候。
技能链接
https://github.com/vudovn/ag-kit/blob/main/.agents/skills/frontend-architecture/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请帮我重新整理这个 React 应用,把接口调用、校验和显示代码分别放进不同的文件。”
每周 5 个新技能,1 分钟读完。
工作原理
技能说明哪类代码应放在哪一层,例如显示、逻辑、数据和类型。AI 在创建或修改文件时,遵循这些规则。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Frontend Architecture
How to organize frontend code so it scales. Separation of concerns over file-type folders. Applies to React/Next and Vue. For directory layout, follow app-builder. For visual design, see frontend-design. For React/Next performance, see nextjs-react-expert.
1. Separation of Concerns — the core rule
Split code into four layers by responsibility. A unit of code does ONE of these, not several:
| Layer | Holds | Lives in |
|---|---|---|
| UI | Rendering, markup, presentational state | components/ |
| Logic | State, effects, data transforms, reusable UI logic | hooks/ (React) · composables/ (Vue) |
| Data | API calls, fetch/axios, cache keys | lib/ / service files (*.api.ts) |
| Type | TypeScript types, domain models | types.ts / *.types.ts |
| Validation | Form/data schemas | *.schema.ts (zod/yup/valibot) |
Directory layout (top-level folders) follows the project's scaffolding skill — do not invent a competing structure here. This skill is about which layer code belongs to, not where the folders sit.
2. File Responsibility & Size
One clear responsibility per file. Size is a signal, not a hard limit — a clear 230-line file beats a 90-line file that fetches, validates, renders, and juggles modals + toasts.
| File type | Comfortable range |
|---|---|
| UI component | 80–180 lines |
| Page / screen | 100–220 lines |
| Hook / composable | 40–150 lines |
| API service | 50–200 lines |
| Type / schema | flexible |
Split a file when it mixes UI + API + business logic + validation + state. Don't split a coherent file just to hit a number.
3. Components render UI; logic goes elsewhere
Components should primarily render. Push fetching/state/transforms into a hook or composable.
// ❌ Component owns the data layer
function ProductList() {
const [products, setProducts] = useState([])
useEffect(() => { fetch('/api/products').then(r => r.json()).then(setProducts) }, [])
return <div>{/* render */}</div>
}
// ✅ Component renders; logic lives in a hook
function ProductList() {
const { products, isLoading } = useProducts()
if (isLoading) return <Loading />
return <div>{/* render */}</div>
}
- Custom hooks must start with
use. - A component calling an API directly is acceptable only for the smallest one-off cases.
4. Next.js — Server Components by default
In the App Router, page.tsx and layout.tsx are Server Components. Reach for "use client" only when you actually need the client.
| Server Component | Client Component |
|---|---|
| Fetch data, read DB/API | Form, modal, dropdown |
| Handle secret tokens | Event handlers, animation |
| Render static/semi-static layout | useState/useEffect, browser APIs (window, localStorage) |
Keep client components small. Don't "use client" a whole page for one interactive button — extract the button into its own client component and keep the page a Server Component.
5. Vue — Composition API + composables
For full production apps, prefer the Composition API with <script setup> Single File Components. (Options API is fine for simple cases / progressive enhancement.)
components/→ UIcomposables/→ reusable pure logic (useX)- service files → API calls
Use a composable for reusable pure logic; use a component when reusing both logic and layout.
6. State — start local, escalate only when needed
| Need | Use |
|---|---|
| Component-internal state | useState / ref |
| Reusable state/logic in one feature | custom hook / composable |
| Shared UI state in a subtree | Context (React) / provide-inject (Vue) |
| Cross-app, complex, persisted | Zustand / Pinia / Redux |
| Server state / API cache | TanStack Query (react-query) / similar |
Don't reach for global state (or Redux) on day one of a small app. Server state belongs in a query library, not a global store.
7. API in service files
Never scatter raw fetch/axios across components.
// user.api.ts
export async function getUsers() {
const res = await http.get('/users')
return res.data
}
// useUsers.ts
export function useUsers() {
return useQuery({ queryKey: ['users'], queryFn: getUsers })
}
Always handle loading, error, and empty states explicitly.
8.
常见问题
它会决定文件夹名称吗?
不会。技能说明,文件夹结构应遵循项目的脚手架指南,它关注的是代码属于哪一层。
它涵盖视觉设计吗?
不涵盖。它讲的是代码结构,视觉设计由另一项技能处理。
文件大小有严格的上限吗?
没有。文件大小只作为一个信号,而不是硬性限制。
详情
作者的原始描述
How to organize frontend code — separation of concerns (UI / logic / data / type), file responsibility, state tiers, API services, schema validation, and framework conventions for React/Next and Vue. Structural rules, not visual design.
repo: vudovn/ag-kit
热度
- GitHub 星标
- 8.2k
- 排名
- #12532
- 许可证
- MIT
- 更新时间
- 2026/10/18
评论
- …