tanstack-start-best-practices

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

TanStack Start Best Practices

TanStack Start 最佳实践

Comprehensive guidelines for implementing TanStack Start patterns in full-stack React applications. These rules cover server functions, middleware, SSR, authentication, and deployment.
为全栈React应用实现TanStack Start模式的综合指南。这些规则涵盖服务器函数、中间件、SSR、身份验证以及部署。

When to Apply

适用场景

  • Creating server functions for data mutations
  • Setting up middleware for auth/logging
  • Configuring SSR and hydration
  • Implementing authentication flows
  • Handling errors across client/server boundary
  • Organizing full-stack code
  • Deploying to various platforms
  • 为数据变更创建服务器函数
  • 设置用于身份验证/日志记录的中间件
  • 配置SSR与水合(hydration)
  • 实现身份验证流程
  • 处理客户端/服务器边界的错误
  • 组织全栈代码
  • 部署到各类平台

Rule Categories by Priority

按优先级划分的规则类别

PriorityCategoryRulesImpact
CRITICALServer Functions5 rulesCore data mutation patterns
CRITICALSecurity4 rulesPrevents vulnerabilities
HIGHMiddleware4 rulesRequest/response handling
HIGHAuthentication4 rulesSecure user sessions
MEDIUMAPI Routes1 ruleExternal endpoint patterns
MEDIUMSSR6 rulesServer rendering patterns
MEDIUMError Handling3 rulesGraceful failure handling
MEDIUMEnvironment1 ruleConfiguration management
LOWFile Organization3 rulesMaintainable code structure
LOWDeployment2 rulesProduction readiness
优先级类别规则数量影响
关键服务器函数5条规则核心数据变更模式
关键安全4条规则预防漏洞
中间件4条规则请求/响应处理
身份验证4条规则安全用户会话
API路由1条规则外部端点模式
SSR6条规则服务器渲染模式
错误处理3条规则优雅的故障处理
环境1条规则配置管理
文件组织3条规则可维护的代码结构
部署2条规则生产就绪性

Quick Reference

快速参考

Server Functions (Prefix:
sf-
)

服务器函数(前缀:
sf-

  • sf-create-server-fn
    — Use createServerFn for server-side logic
  • sf-input-validation
    — Always validate server function inputs
  • sf-method-selection
    — Choose appropriate HTTP method
  • sf-error-handling
    — Handle errors in server functions
  • sf-response-headers
    — Customize response headers when needed
  • sf-create-server-fn
    — 使用createServerFn实现服务器端逻辑
  • sf-input-validation
    — 始终验证服务器函数的输入
  • sf-method-selection
    — 选择合适的HTTP方法
  • sf-error-handling
    — 处理服务器函数中的错误
  • sf-response-headers
    — 必要时自定义响应头

Security (Prefix:
sec-
)

安全(前缀:
sec-

  • sec-validate-inputs
    — Validate all user inputs with schemas
  • sec-auth-middleware
    — Protect routes with auth middleware
  • sec-sensitive-data
    — Keep secrets server-side only
  • sec-csrf-protection
    — Implement CSRF protection for mutations
  • sec-validate-inputs
    — 使用验证规则校验所有用户输入
  • sec-auth-middleware
    — 用身份验证中间件保护路由
  • sec-sensitive-data
    — 敏感数据仅保留在服务器端
  • sec-csrf-protection
    — 为数据变更实现CSRF保护

Middleware (Prefix:
mw-
)

中间件(前缀:
mw-

  • mw-request-middleware
    — Use request middleware for cross-cutting concerns
  • mw-function-middleware
    — Use function middleware for server functions
  • mw-context-flow
    — Properly pass context through middleware
  • mw-composability
    — Compose middleware effectively
  • mw-request-middleware
    — 使用请求中间件处理横切关注点
  • mw-function-middleware
    — 为服务器函数使用函数中间件
  • mw-context-flow
    — 通过中间件正确传递上下文
  • mw-composability
    — 有效组合中间件

Authentication (Prefix:
auth-
)

身份验证(前缀:
auth-

  • auth-session-management
    — Implement secure session handling
  • auth-route-protection
    — Protect routes with beforeLoad
  • auth-server-functions
    — Verify auth in server functions
  • auth-cookie-security
    — Configure secure cookie settings
  • auth-session-management
    — 实现安全的会话处理
  • auth-route-protection
    — 使用beforeLoad保护路由
  • auth-server-functions
    — 在服务器函数中验证身份
  • auth-cookie-security
    — 配置安全的Cookie设置

API Routes (Prefix:
api-
)

API路由(前缀:
api-

  • api-routes
    — Create API routes for external consumers
  • api-routes
    — 为外部消费者创建API路由

SSR (Prefix:
ssr-
)

SSR(前缀:
ssr-

  • ssr-data-loading
    — Load data appropriately for SSR
  • ssr-hydration-safety
    — Prevent hydration mismatches
  • ssr-streaming
    — Implement streaming SSR for faster TTFB
  • ssr-selective
    — Apply selective SSR when beneficial
  • ssr-prerender
    — Configure static prerendering and ISR
  • ssr-data-loading
    — 为SSR适当加载数据
  • ssr-hydration-safety
    — 避免水合不匹配
  • ssr-streaming
    — 实现流式SSR以加快TTFB
  • ssr-selective
    — 在有益时应用选择性SSR
  • ssr-prerender
    — 配置静态预渲染和ISR

Environment (Prefix:
env-
)

环境(前缀:
env-

  • env-functions
    — Use environment functions for configuration
  • env-functions
    — 使用环境函数进行配置

Error Handling (Prefix:
err-
)

错误处理(前缀:
err-

  • err-server-errors
    — Handle server function errors
  • err-redirects
    — Use redirects appropriately
  • err-not-found
    — Handle not-found scenarios
  • err-server-errors
    — 处理服务器函数错误
  • err-redirects
    — 合理使用重定向
  • err-not-found
    — 处理未找到场景

File Organization (Prefix:
file-
)

文件组织(前缀:
file-

  • file-separation
    — Separate server and client code
  • file-functions-file
    — Use .functions.ts pattern
  • file-shared-validation
    — Share validation schemas
  • file-separation
    — 分离服务器和客户端代码
  • file-functions-file
    — 使用.functions.ts模式
  • file-shared-validation
    — 共享验证规则

Deployment (Prefix:
deploy-
)

部署(前缀:
deploy-

  • deploy-env-config
    — Configure environment variables
  • deploy-adapters
    — Choose appropriate deployment adapter
  • deploy-env-config
    — 配置环境变量
  • deploy-adapters
    — 选择合适的部署适配器

How to Use

使用方法

Each rule file in the
rules/
directory contains:
  1. Explanation — Why this pattern matters
  2. Bad Example — Anti-pattern to avoid
  3. Good Example — Recommended implementation
  4. Context — When to apply or skip this rule
rules/
目录下的每个规则文件包含:
  1. 说明 — 该模式的重要性
  2. 反面示例 — 需要避免的反模式
  3. 正面示例 — 推荐的实现方式
  4. 适用场景 — 何时应用或跳过该规则

Full Reference

完整参考

See individual rule files in
rules/
directory for detailed guidance and code examples.
请查看
rules/
目录下的单个规则文件,获取详细指导和代码示例。