nuxt-best-practices
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseNuxt Best Practices
Nuxt最佳实践
Comprehensive performance optimization guide for Nuxt 3/4 applications. Contains 40+ rules across 8 categories, prioritized by impact to guide automated refactoring and code generation.
针对Nuxt 3/4应用的全面性能优化指南。包含8个分类下的40+条规则,按影响优先级排序,可指导自动化重构与代码生成。
When to Apply
适用场景
Reference these guidelines when:
- Writing new Nuxt pages, components, or composables
- Implementing data fetching (useFetch, useAsyncData)
- Creating server routes and API endpoints
- Organizing types, composables, and auto-imports
- Working with Nuxt modules and plugins
- Configuring rendering modes (SSR, SSG, SPA)
在以下场景中参考本指南:
- 编写新的Nuxt页面、组件或组合式函数(composables)
- 实现数据获取逻辑(useFetch、useAsyncData)
- 创建服务端路由与API端点
- 组织类型定义、组合式函数与自动导入内容
- 开发Nuxt模块与插件
- 配置渲染模式(SSR、SSG、SPA)
Rule Categories by Priority
按优先级划分的规则分类
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Data Fetching | CRITICAL | |
| 2 | Auto-Imports & Organization | CRITICAL | |
| 3 | Server & API Routes | HIGH | |
| 4 | Rendering Modes | HIGH | |
| 5 | State Management | MEDIUM-HIGH | |
| 6 | Type Safety | MEDIUM | |
| 7 | Modules & Plugins | LOW-MEDIUM | |
| 8 | Performance & Deployment | LOW | |
| 优先级 | 分类 | 影响程度 | 前缀 |
|---|---|---|---|
| 1 | 数据获取 | 关键 | |
| 2 | 自动导入与代码组织 | 关键 | |
| 3 | 服务端与API路由 | 高 | |
| 4 | 渲染模式 | 高 | |
| 5 | 状态管理 | 中高 | |
| 6 | 类型安全 | 中 | |
| 7 | 模块与插件 | 中低 | |
| 8 | 性能与部署 | 低 | |
Quick Reference
快速参考
1. Data Fetching (CRITICAL)
1. 数据获取(关键)
- - Use useFetch/useAsyncData, never raw fetch in components
data-use-fetch - - Always provide unique keys for data fetching
data-key-unique - - Use lazy option for non-critical data
data-lazy-loading - - Transform data at fetch time, not in template
data-transform - - Always handle error and pending states
data-error-handling - - Use refresh() and clear() appropriately
data-refresh-patterns
- - 使用useFetch/useAsyncData,切勿在组件中直接使用原生fetch
data-use-fetch - - 始终为数据获取提供唯一的key
data-key-unique - - 对非关键数据使用lazy选项
data-lazy-loading - - 在获取数据时进行转换,而非在模板中处理
data-transform - - 始终处理错误与加载状态
data-error-handling - - 合理使用refresh()与clear()方法
data-refresh-patterns
2. Auto-Imports & Organization (CRITICAL)
2. 自动导入与代码组织(关键)
- - Never create barrel exports in auto-imported directories
imports-no-barrel-autoimport - - Don't duplicate folder prefix in component names
imports-component-naming - - Place types in dedicated directories (app/types, shared/types, server/types)
imports-type-locations - - Composables export functions only, not types
imports-composable-exports - - Use direct imports between composables
imports-direct-composable-imports
- - 切勿在自动导入目录中创建桶式导出(barrel exports)
imports-no-barrel-autoimport - - 组件名称中不要重复文件夹前缀
imports-component-naming - - 将类型定义放在专用目录中(app/types、shared/types、server/types)
imports-type-locations - - 组合式函数仅导出函数,不导出类型
imports-composable-exports - - 组合式函数之间使用直接导入
imports-direct-composable-imports
3. Server & API Routes (HIGH)
3. 服务端与API路由(高)
- - Use getValidatedQuery/readValidatedBody with Zod
server-validated-input - - Always add defineRouteMeta for OpenAPI docs
server-route-meta - - Use useRuntimeConfig, never process.env
server-runtime-config - - Use createError for consistent error responses
server-error-handling - - Understand middleware execution order
server-middleware-order
- - 结合Zod使用getValidatedQuery/readValidatedBody
server-validated-input - - 始终添加defineRouteMeta以生成OpenAPI文档
server-route-meta - - 使用useRuntimeConfig,切勿直接使用process.env
server-runtime-config - - 使用createError生成一致的错误响应
server-error-handling - - 理解中间件的执行顺序
server-middleware-order
4. Rendering Modes (HIGH)
4. 渲染模式(高)
- - Configure rendering per-route with routeRules
rendering-route-rules - - Use hybrid rendering for optimal performance
rendering-hybrid - - Prerender static pages at build time
rendering-prerender - - Use ClientOnly for browser-specific components
rendering-client-only
- - 通过routeRules为单个路由配置渲染模式
rendering-route-rules - - 使用混合渲染以获得最优性能
rendering-hybrid - - 在构建时预渲染静态页面
rendering-prerender - - 对浏览器专属组件使用ClientOnly
rendering-client-only
5. State Management (MEDIUM-HIGH)
5. 状态管理(中高)
- - Use useState for SSR-safe shared state
state-use-state - - Set up Pinia correctly with Nuxt
state-pinia-setup - - Handle hydration mismatches properly
state-hydration - - Prefer computed over watch for derived state
state-computed-over-watch
- - 使用useState实现支持SSR的共享状态
state-use-state - - 正确在Nuxt中配置Pinia
state-pinia-setup - - 妥善处理水合不匹配问题
state-hydration - - 优先使用computed而非watch处理派生状态
state-computed-over-watch
6. Type Safety (MEDIUM)
6. 类型安全(中)
- - Never define types inline in components/composables
types-no-inline - - Use correct import paths (#shared, ~/, ~~/)
types-import-paths - - Never use
types-no-anytypeany - - Use Zod for runtime validation with type inference
types-zod-schemas - - Use kebab-case emits with full type definitions
types-strict-emits
- - 切勿在组件/组合式函数中内联定义类型
types-no-inline - - 使用正确的导入路径(#shared、~/、~~/)
types-import-paths - - 切勿使用
types-no-any类型any - - 使用Zod进行运行时验证并实现类型推断
types-zod-schemas - - 使用短横线命名法(kebab-case)的emits并提供完整类型定义
types-strict-emits
7. Modules & Plugins (LOW-MEDIUM)
7. 模块与插件(中低)
- - Module order matters in nuxt.config
modules-order - - Understand runtime vs build-time modules
modules-runtime-vs-build - - Use .client.ts and .server.ts suffixes correctly
plugins-client-server - - Use provide/inject for cross-cutting concerns
plugins-provide-inject
- - nuxt.config中的模块顺序至关重要
modules-order - - 区分运行时模块与构建时模块
modules-runtime-vs-build - - 正确使用.client.ts与.server.ts后缀
plugins-client-server - - 使用provide/inject处理横切关注点
plugins-provide-inject
8. Performance & Deployment (LOW)
8. 性能与部署(低)
- - Analyze and optimize bundle size
perf-bundle-analysis - - Use nuxt/image for optimized images
perf-image-optimization - - Configure font loading strategy
perf-font-loading - - Set appropriate cache headers
perf-caching-headers
- - 分析并优化包体积
perf-bundle-analysis - - 使用nuxt/image优化图片
perf-image-optimization - - 配置字体加载策略
perf-font-loading - - 设置合适的缓存头
perf-caching-headers
How to Use
使用方法
Read individual rule files for detailed explanations and code examples:
rules/data-use-fetch.md
rules/imports-no-barrel-autoimport.md
rules/_sections.mdEach rule file contains:
- Brief explanation of why it matters
- Incorrect code example with explanation
- Correct code example with explanation
- Additional context and Nuxt-specific notes
阅读单个规则文件以获取详细说明与代码示例:
rules/data-use-fetch.md
rules/imports-no-barrel-autoimport.md
rules/_sections.md每个规则文件包含:
- 规则重要性的简要说明
- 错误代码示例及解释
- 正确代码示例及解释
- 额外说明与Nuxt专属注意事项
Full Compiled Document
完整编译文档
For the complete guide with all rules expanded:
AGENTS.md如需查看包含所有规则详情的完整指南,请参考:
AGENTS.md