nuqs
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCommunity nuqs Best Practices for Next.js
社区版 nuqs 在 Next.js 中的最佳实践
Comprehensive guide for type-safe URL query state management with nuqs in Next.js applications. Contains 42 rules across 8 categories, prioritized by impact to guide code generation, refactoring, and code review.
本指南全面介绍了在Next.js应用中使用nuqs进行类型安全的URL查询状态管理的方法。包含8个类别下的42条规则,按影响级别排序,可指导代码生成、重构和代码评审工作。
When to Apply
适用场景
Reference these guidelines when:
- Implementing URL-based state with nuqs
- Setting up nuqs in a Next.js project
- Configuring parsers for URL parameters
- Integrating URL state with Server Components
- Optimizing URL update performance
- Debugging nuqs-related issues
在以下场景中可参考本指南:
- 使用nuqs实现基于URL的状态管理
- 在Next.js项目中配置nuqs
- 为URL参数配置解析器
- 将URL状态与Server Components集成
- 优化URL更新性能
- 调试与nuqs相关的问题
Rule Categories by Priority
按优先级划分的规则类别
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Parser Configuration | CRITICAL | |
| 2 | Adapter & Setup | CRITICAL | |
| 3 | State Management | HIGH | |
| 4 | Server Integration | HIGH | |
| 5 | Performance Optimization | MEDIUM | |
| 6 | History & Navigation | MEDIUM | |
| 7 | Debugging & Testing | LOW-MEDIUM | |
| 8 | Advanced Patterns | LOW | |
| 优先级 | 类别 | 影响级别 | 前缀 |
|---|---|---|---|
| 1 | 解析器配置 | 关键 | |
| 2 | 适配器与设置 | 关键 | |
| 3 | 状态管理 | 高 | |
| 4 | 服务端集成 | 高 | |
| 5 | 性能优化 | 中 | |
| 6 | 历史记录与导航 | 中 | |
| 7 | 调试与测试 | 低-中 | |
| 8 | 高级模式 | 低 | |
Quick Reference
快速参考
1. Parser Configuration (CRITICAL)
1. 解析器配置(关键)
- - Use typed parsers for non-string values
parser-use-typed-parsers - - Use withDefault for non-nullable state
parser-with-default - - Use enum parsers for constrained values
parser-enum-validation - - Choose correct array parser format
parser-array-format - - Validate JSON parser input
parser-json-validation - - Select appropriate date parser
parser-date-format - - Use parseAsIndex for 1-based URL display
parser-index-offset - - Use parseAsHex for color values
parser-hex-colors
- - 为非字符串值使用类型化解析器
parser-use-typed-parsers - - 为非空状态使用withDefault
parser-with-default - - 为受限值使用枚举解析器
parser-enum-validation - - 选择正确的数组解析器格式
parser-array-format - - 验证JSON解析器的输入
parser-json-validation - - 选择合适的日期解析器
parser-date-format - - 使用parseAsIndex实现基于1的URL显示
parser-index-offset - - 为颜色值使用parseAsHex
parser-hex-colors
2. Adapter & Setup (CRITICAL)
2. 适配器与设置(关键)
- - Wrap app with NuqsAdapter
setup-nuqs-adapter - - Add 'use client' directive for hooks
setup-use-client - - Import server utilities from nuqs/server
setup-import-server - - Ensure compatible Next.js version
setup-nextjs-version - - Define shared parsers in dedicated file
setup-shared-parsers
- - 使用NuqsAdapter包裹应用
setup-nuqs-adapter - - 为钩子添加'use client'指令
setup-use-client - - 从nuqs/server导入服务端工具
setup-import-server - - 确保使用兼容的Next.js版本
setup-nextjs-version - - 在专用文件中定义共享解析器
setup-shared-parsers
3. State Management (HIGH)
3. 状态管理(高)
- - Use useQueryStates for related parameters
state-use-query-states - - Use functional updates for derived state
state-functional-updates - - Clear URL parameters with null
state-clear-with-null - - Handle controlled input value properly
state-controlled-inputs - - Avoid derived state from URL parameters
state-avoid-derived - - Use withOptions for parser-level configuration
state-options-inheritance - - Use setter return value for URL access
state-setter-return
- - 为相关参数使用useQueryStates
state-use-query-states - - 为派生状态使用函数式更新
state-functional-updates - - 使用null清除URL参数
state-clear-with-null - - 正确处理受控输入值
state-controlled-inputs - - 避免从URL参数派生状态
state-avoid-derived - - 使用withOptions进行解析器级配置
state-options-inheritance - - 使用设置器的返回值访问URL
state-setter-return
4. Server Integration (HIGH)
4. 服务端集成(高)
- - Use createSearchParamsCache for Server Components
server-search-params-cache - - Use shallow:false to trigger server re-renders
server-shallow-false - - Integrate useTransition for loading states
server-use-transition - - Call parse() before get() in Server Components
server-parse-before-get - - Share parsers between client and server
server-share-parsers - - Handle async searchParams in Next.js 15+
server-next15-async
- - 为Server Components使用createSearchParamsCache
server-search-params-cache - - 使用shallow:false触发服务端重渲染
server-shallow-false - - 集成useTransition处理加载状态
server-use-transition - - 在Server Components中先调用parse()再调用get()
server-parse-before-get - - 在客户端与服务端之间共享解析器
server-share-parsers - - 在Next.js 15+中处理异步searchParams
server-next15-async
5. Performance Optimization (MEDIUM)
5. 性能优化(中)
- - Throttle rapid URL updates
perf-throttle-updates - - Use clearOnDefault for clean URLs
perf-clear-on-default - - Memoize components using URL state
perf-avoid-rerender - - Use createSerializer for link URLs
perf-serialize-utility - - Debounce search input before URL update
perf-debounce-search
- - 限制频繁的URL更新
perf-throttle-updates - - 使用clearOnDefault保持URL简洁
perf-clear-on-default - - 对使用URL状态的组件进行记忆化处理
perf-avoid-rerender - - 使用createSerializer生成链接URL
perf-serialize-utility - - 在更新URL前对搜索输入进行防抖处理
perf-debounce-search
6. History & Navigation (MEDIUM)
6. 历史记录与导航(中)
- - Use history:push for navigation-like state
history-push-navigation - - Use history:replace for ephemeral state
history-replace-ephemeral - - Control scroll behavior on URL changes
history-scroll-behavior - - Handle browser back/forward navigation
history-back-sync
- - 对类导航状态使用history:push
history-push-navigation - - 对临时状态使用history:replace
history-replace-ephemeral - - 控制URL变化时的滚动行为
history-scroll-behavior - - 处理浏览器前进/后退导航
history-back-sync
7. Debugging & Testing (LOW-MEDIUM)
7. 调试与测试(低-中)
- - Enable debug logging for troubleshooting
debug-enable-logging - - Diagnose common nuqs errors
debug-common-errors - - Test components with URL state
debug-testing
- - 启用调试日志以排查问题
debug-enable-logging - - 诊断常见的nuqs错误
debug-common-errors - - 测试包含URL状态的组件
debug-testing
8. Advanced Patterns (LOW)
8. 高级模式(低)
- - Create custom parsers for complex types
advanced-custom-parsers - - Use urlKeys for shorter URLs
advanced-url-keys - - Implement eq function for object parsers
advanced-eq-function - - Use framework-specific adapters
advanced-framework-adapters
- - 为复杂类型创建自定义解析器
advanced-custom-parsers - - 使用urlKeys缩短URL
advanced-url-keys - - 为对象解析器实现eq函数
advanced-eq-function - - 使用框架特定的适配器
advanced-framework-adapters
How to Use
使用方法
Read individual reference files for detailed explanations and code examples:
- Section definitions - Category structure and impact levels
- Rule template - Template for adding new rules
阅读单个参考文件获取详细说明和代码示例:
- 章节定义 - 类别结构和影响级别
- 规则模板 - 添加新规则的模板
Reference Files
参考文件
| File | Description |
|---|---|
| AGENTS.md | Complete compiled guide with all rules |
| references/_sections.md | Category definitions and ordering |
| assets/templates/_template.md | Template for new rules |
| metadata.json | Version and reference information |
| 文件 | 描述 |
|---|---|
| AGENTS.md | 包含所有规则的完整编译指南 |
| references/_sections.md | 类别定义和排序规则 |
| assets/templates/_template.md | 新规则的模板 |
| metadata.json | 版本和参考信息 |