tanstack-start-architecture
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTanStack Start Architecture Enforcement
TanStack Start架构规范校验
Overview
概述
Enforces hypercore TanStack Start architecture rules with 100% compliance. Validates project structure, then applies strict layer/route/hook/convention rules to every code change.
This skill is RIGID. Follow exactly. No exceptions.
REQUIRED: When working on TanStack Start projects, ALWAYS use this skill together with to guarantee 100% architecture compliance. Ralph's persistence loop ensures every rule is verified and no violation slips through.
/oh-my-claudecode:ralph100%合规地强制执行核心TanStack Start架构规则,先校验项目结构,再对每一次代码变更应用严格的分层/路由/hook/约定规则。
本规范极其严格,必须完全遵守,无任何例外。
要求: 开发TanStack Start项目时,必须始终结合使用本规范,以确保100%符合架构要求。Ralph的持久化循环会校验所有规则,杜绝任何违规情况漏过。
/oh-my-claudecode:ralphStep 1: Project Validation
步骤1:项目校验
Before any work, confirm TanStack Start project:
bash
undefined开展任何工作前,先确认是TanStack Start项目:
bash
undefinedCheck for TanStack Start indicators (ANY of these)
检查TanStack Start标识(满足任意一项即可)
ls app.config.ts 2>/dev/null # TanStack Start config
grep -r "@tanstack/react-start" package.json 2>/dev/null
grep -r "@tanstack/react-router" package.json 2>/dev/null
ls src/routes/__root.tsx 2>/dev/null
If NONE found: **STOP. This skill does not apply.** Inform user.
If found: proceed with architecture enforcement.ls app.config.ts 2>/dev/null # TanStack Start配置文件
grep -r "@tanstack/react-start" package.json 2>/dev/null
grep -r "@tanstack/react-router" package.json 2>/dev/null
ls src/routes/__root.tsx 2>/dev/null
如果未找到任何标识:**停止操作,本规范不适用于当前项目,请告知用户。**
如果找到标识:继续执行架构校验流程。Step 2: Read Architecture Rules
步骤2:阅读架构规则
Load the detailed rules reference:
REQUIRED: Read in this skill directory before writing any code.
architecture-rules.mdFor detailed patterns and examples, also read the relevant reference files:
- - Code conventions (naming, TypeScript, imports, comments)
rules/conventions.md - - Route structure (folder rules, patterns, loaders)
rules/routes.md - - Server Functions (schemas, queries, mutations, middleware)
rules/services.md - - Custom Hook patterns (separation rules, internal order)
rules/hooks.md
加载详细的规则参考文档:
要求: 编写任何代码前,必须先阅读本技能目录下的文件。
architecture-rules.md如需了解详细模式和示例,还可阅读对应的参考文件:
- - 代码约定(命名、TypeScript、导入、注释)
rules/conventions.md - - 路由结构(文件夹规则、模式、loaders)
rules/routes.md - - Server Functions(schema、查询、变更、中间件)
rules/services.md - - 自定义Hook模式(分层规则、内部顺序)
rules/hooks.md
Step 3: Pre-Change Validation Checklist
步骤3:变更前校验清单
Before writing ANY code, verify your planned change against these gates:
编写任何代码前,对照以下检查项确认你计划的变更符合要求:
Gate 1: Layer Violations
检查项1:分层违规
Routes -> Server Functions -> Features -> Database| Check | Rule |
|---|---|
| Route accessing DB directly? | BLOCKED. Must go through Server Functions -> Features |
| Route calling Prisma? | BLOCKED. Use Server Functions |
| Server Function skipping Features? | ALLOWED only for simple CRUD |
| Client calling Server Function directly? | BLOCKED. Use TanStack Query (exception: |
Routes -> Server Functions -> Features -> Database| 检查项 | 规则 |
|---|---|
| 路由是否直接访问数据库? | 禁止,必须走Server Functions -> Features层 |
| 路由是否调用Prisma? | 禁止,请使用Server Functions |
| Server Function是否跳过Features层? | 仅简单CRUD场景允许 |
| 客户端是否直接调用Server Function? | 禁止,请使用TanStack Query(例外: |
Gate 2: Route Structure
检查项2:路由结构
| Check | Rule |
|---|---|
Flat file route? ( | BLOCKED. Use folder ( |
Missing | BLOCKED. Every page needs it |
Missing | BLOCKED. Every page needs it |
Missing | BLOCKED. Every page needs it |
| BLOCKED. Must be |
| Logic in page component? | BLOCKED. Extract to |
Layout route missing | BLOCKED. Routes needing beforeLoad/loader must have |
Route with search params but no | BLOCKED. Must use |
Route without | WARNING. Recommended for all routes with loaders |
| 检查项 | 规则 |
|---|---|
是否为扁平文件路由?( | 禁止,请使用文件夹结构( |
是否缺失 | 禁止,每个页面必须包含该文件夹 |
是否缺失 | 禁止,每个页面必须包含该文件夹 |
是否缺失 | 禁止,每个页面必须包含该文件夹 |
| 禁止,必须写为 |
| 页面组件中是否包含业务逻辑? | 禁止,提取到 |
布局路由是否缺失 | 禁止,需要使用beforeLoad/loader的路由必须有 |
路由使用搜索参数但没有配置 | 禁止,必须结合 |
路由没有配置 | 警告,建议所有带loader的路由都配置该属性 |
Gate 3: Server Functions
检查项3:Server Functions
| Check | Rule |
|---|---|
POST/PUT/PATCH without | BLOCKED |
Auth-required without | BLOCKED |
Using | BLOCKED. |
| handler not last in chain? | BLOCKED. handler must ALWAYS be last (middleware/inputValidator order is flexible) |
Missing | BLOCKED. Use |
| Direct server function call in component? | BLOCKED. Use |
| BLOCKED. Tree shaking failure |
| 检查项 | 规则 |
|---|---|
POST/PUT/PATCH请求是否没有配置 | 禁止 |
需要鉴权的接口是否没有配置 | 禁止 |
是否使用 | 禁止, |
| handler是否不在调用链末尾? | 禁止,handler必须始终放在最后(middleware/inputValidator的顺序可灵活调整) |
搜索参数是否缺失 | 禁止,请使用 |
| 组件中是否直接调用Server Function? | 禁止,请使用 |
是否存在 | 禁止,会导致tree shaking失效 |
Gate 4: Hooks
检查项4:Hooks
| Check | Rule |
|---|---|
| Hook inside page component? | BLOCKED. Must be in |
| Wrong hook order? | BLOCKED. State -> Global -> Server Fns -> Query -> Handlers -> Memo -> Effect |
| Missing return type interface? | BLOCKED |
| camelCase hook filename? | BLOCKED. Use |
| 检查项 | 规则 |
|---|---|
| Hook是否写在页面组件内部? | 禁止,必须放在 |
| Hook内部调用顺序是否错误? | 禁止,顺序必须为:State -> Global -> Server Fns -> Query -> Handlers -> Memo -> Effect |
| 是否缺失返回类型定义? | 禁止 |
| Hook文件名是否为驼峰命名? | 禁止,请使用 |
Gate 5: Conventions
检查项5:开发约定
| Check | Rule |
|---|---|
| camelCase filename? | BLOCKED. Use kebab-case |
| BLOCKED. Use const arrow function |
| BLOCKED. Use |
| Missing explicit return type? | BLOCKED |
| Wrong import order? | BLOCKED. External -> @/ -> Relative -> Type |
| Missing Korean block comments? | BLOCKED for code groups |
Using | BLOCKED. Use Zod 4.x |
| 检查项 | 规则 |
|---|---|
| 文件名是否为驼峰命名? | 禁止,请使用短横线分隔命名 |
是否使用 | 禁止,请使用const箭头函数 |
是否使用 | 禁止,请使用 |
| 是否缺失显式返回类型? | 禁止 |
| 导入顺序是否错误? | 禁止,顺序必须为:外部依赖 -> @/别名导入 -> 相对路径导入 -> 类型导入 |
| 是否缺失韩文块注释? | 代码组必须添加,禁止缺失 |
是否使用 | 禁止,请直接使用Zod 4.x的 |
Step 4: Implementation (with Ralph)
步骤4:落地实现(结合Ralph使用)
When used with ralph, every PRD story MUST include these acceptance criteria:
- [ ] Layer architecture respected (no layer skipping)
- [ ] Route uses folder structure with -components/, -hooks/, -functions/
- [ ] export const Route = createFileRoute(...) used
- [ ] Server Functions use correct chaining (handler always last, middleware/inputValidator flexible)
- [ ] Search params use zodValidator from @tanstack/zod-adapter
- [ ] Custom Hooks in -hooks/ with correct internal order
- [ ] All filenames kebab-case
- [ ] Korean block comments present
- [ ] const arrow functions with explicit return types结合ralph使用时,每个PRD需求必须包含以下验收标准:
- [ ] 遵守分层架构要求(无跨层调用)
- [ ] 路由使用文件夹结构,包含`-components/`、`-hooks/`、`-functions/`
- [ ] 使用`export const Route = createFileRoute(...)`写法
- [ ] Server Functions使用正确的调用链(handler始终在末尾,middleware/inputValidator顺序灵活)
- [ ] 搜索参数使用`@tanstack/zod-adapter`提供的`zodValidator`
- [ ] 自定义Hook放在`-hooks/`文件夹中,内部调用顺序正确
- [ ] 所有文件名使用短横线分隔命名
- [ ] 存在韩文块注释
- [ ] 使用const箭头函数并声明显式返回类型Step 5: Post-Change Verification
步骤5:变更后校验
After writing code, verify:
- Structure check: the route folder - confirm
ls,-components/,-hooks/exist-functions/ - Export check: grep for in route files
export const Route - Layer check: no Prisma imports in route files
- Convention check: no camelCase filenames, no keyword declarations
function - Hook order check: read hook files, verify State -> Global -> Server Fns -> Query -> Handlers -> Memo -> Effect
编写完代码后,校验以下内容:
- 结构校验:执行查看路由文件夹,确认
ls、-components/、-hooks/存在-functions/ - 导出校验:grep检查路由文件中是否包含
export const Route - 分层校验:路由文件中没有导入Prisma相关内容
- 约定校验:无驼峰命名的文件名,无关键字声明的函数
function - Hook顺序校验:查看Hook文件,确认顺序符合State -> Global -> Server Fns -> Query -> Handlers -> Memo -> Effect要求
Quick Reference: Folder Structure
快速参考:文件夹结构
src/
├── routes/ # File-based routing
│ └── <page>/
│ ├── index.tsx # Page (UI only)
│ ├── route.tsx # Layout (beforeLoad, loader)
│ ├── -components/ # REQUIRED: page components
│ ├── -hooks/ # REQUIRED: page hooks (ALL logic here)
│ ├── -functions/ # REQUIRED: page server functions
│ └── -sections/ # Optional: 200+ line pages
├── features/<domain>/ # Internal domain (Prisma queries)
│ ├── schemas.ts
│ ├── queries.ts
│ └── mutations.ts
├── services/<provider>/ # External SDK wrappers
├── functions/ # Global server functions (NO index.ts!)
│ └── middlewares/
├── database/ # Prisma client singleton
├── stores/ # Zustand stores
├── hooks/ # Global hooks
├── components/ # Shared UI
│ ├── ui/ # shadcn/ui
│ ├── layout/ # Header, Sidebar, Footer
│ └── shared/ # Common components
├── types/ # Global types
├── env/ # t3-env validation
├── config/ # auth, query-client, sentry
└── lib/ # Utilities
├── utils/
├── constants/
└── validators/src/
├── routes/ # 基于文件的路由
│ └── <page>/
│ ├── index.tsx # 页面(仅UI代码)
│ ├── route.tsx # 布局(beforeLoad、loader逻辑)
│ ├── -components/ # 必填:页面专属组件
│ ├── -hooks/ # 必填:页面专属hooks(所有逻辑放在此处)
│ ├── -functions/ # 必填:页面专属server functions
│ └── -sections/ # 可选:代码超过200行的页面拆分模块
├── features/<domain>/ # 内部领域层(Prisma查询逻辑)
│ ├── schemas.ts
│ ├── queries.ts
│ └── mutations.ts
├── services/<provider>/ # 外部SDK封装层
├── functions/ # 全局server functions(禁止添加index.ts!)
│ └── middlewares/
├── database/ # Prisma client单例
├── stores/ # Zustand stores
├── hooks/ # 全局hooks
├── components/ # 共享UI组件
│ ├── ui/ # shadcn/ui组件
│ ├── layout/ # 头部、侧边栏、底部组件
│ └── shared/ # 通用组件
├── types/ # 全局类型定义
├── env/ # t3-env校验
├── config/ # auth、query-client、sentry配置
└── lib/ # 工具函数
├── utils/
├── constants/
└── validators/Common Mistakes
常见错误
| Mistake | Fix |
|---|---|
| |
| |
| |
| Logic in page component | Extract to |
| Use |
| Import directly from individual files |
| Hook with mixed order | Follow: State -> Global -> Server Fns -> Query -> Handlers -> Memo -> Effect |
| |
| |
Direct Zod schema in | Use |
Server function call without | Use |
| Use |
Missing | Add |
| 错误写法 | 修复方案 |
|---|---|
| |
| |
| |
| 页面组件中包含业务逻辑 | 提取到 |
| 使用 |
| 直接从单个文件导入 |
| Hook内部调用顺序混乱 | 遵循:State -> Global -> Server Fns -> Query -> Handlers -> Memo -> Effect |
| |
| |
| 使用 |
不通过 | 组件中使用 |
| 使用 |
路由缺失 | 添加 |
Red Flags - STOP and Fix
危险信号 - 立即停止并修复
- Route file importing from directly
@/database/prisma - Missing on
exportconst Route - Page component with ,
useStateetc. inline (not in hook)useQuery - Server function using instead of
.validator().inputValidator() - type anywhere
any - camelCase filenames
- route handlers (use Server Functions)
/api - Missing folder in any route
-hooks/ - Route using search params without
validateSearch - Component calling server function directly (not through )
useServerFn - without
createMiddleware()option{ type: 'function' }
- 路由文件直接从导入
@/database/prisma - 缺少
const Route关键字export - 页面组件内直接写、
useState等逻辑(未封装到hook中)useQuery - Server Function使用而非
.validator().inputValidator() - 任何位置使用类型
any - 文件名使用驼峰命名
- 存在路由处理器(请使用Server Functions)
/api - 任意路由缺失文件夹
-hooks/ - 路由使用搜索参数但未配置
validateSearch - 组件直接调用Server Function(未通过)
useServerFn - 未传
createMiddleware()参数{ type: 'function' }