vercel-labs/agent-skills

vercel-react-best-practices

Vercel 编写的 React 与 Next.js 性能检查指南,按影响安排代码审查顺序。

查看源码

详细说明

这份 Skill 的价值在于安排性能审查的顺序。当前固定版本把 70 条规则分为八类,将请求瀑布和包体积列为最高优先级,再覆盖服务端工作、客户端取数、重复渲染、渲染过程、JavaScript 和进阶模式。检查一个页面时,可以先找依赖关系和加载工作带来的成本,再决定是否值得调整较小的局部写法。

SKILL.md 是入口索引,具体解释分布在 rules/ 目录的规则文件中,AGENTS.md 则提供展开后的完整指南。规则文件会说明问题并给出正反代码示例,例如 async-parallel 讨论独立操作的并行处理,bundle-barrel-imports 关注导入结构。应用建议前应打开对应说明,结合组件的数据依赖、鉴权边界和框架版本判断,不能只凭优先级标签决定改代码。

需要准备什么

  • 项目使用的 React 或 Next.js 版本、相关页面与组件文件,以及希望检查的具体任务。
  • 与该任务关联的数据请求和导入代码;已有的包体积分析或性能诊断可一并提供。
  • 必须保留的行为,包括请求依赖、服务端与客户端边界、用户权限要求。

可以得到什么

  • 对应具体规则及影响分类的代码审查建议。
  • 用于判断改动是否合适的代码模式和规则文件示例。
  • 需要结合项目构建、行为检查和性能测量验证的候选优化项。

使用步骤

  1. 明确目标页面或组件、框架版本,以及要排查的行为或加载问题。
  2. 先查看请求瀑布和包体积类别,再检查与当前代码相关的其他规则。
  3. 打开对应规则文件或 AGENTS.md 中的展开说明,不只阅读入口的一句话索引。
  4. 核对建议是否符合实际依赖、请求作用域、用户权限和可用 API。
  5. 如需在自己的项目中改代码,逐项进行小范围调整,并在相同条件下比较行为和已有诊断结果。

限制与检查

  • 被并行处理的操作确实独立,必要的数据与鉴权顺序没有被打乱。
  • 导入和延迟加载调整符合组件的服务端或客户端边界。
  • 缓存不会引入跨请求共享的可变状态,也不会混用不同用户的数据。
  • 项目实际支持所选规则中涉及的 React 或 Next.js API。
  • 改动经过测试和测量验证,不能把规则的影响标签当成已经测出的性能收益。

常见问题

只有 Next.js 项目才适用吗?

来源同时覆盖 React 与 Next.js。组件状态、重复渲染和 JavaScript 等规则可用于相关 React 代码;服务端操作及其他框架专属模式需要对应环境。应选择适用的规则,不必把整份清单当成所有项目都必须完成的改造任务。

只复制 SKILL.md 就够了吗?

入口文档会引用单独的规则文件和展开后的 AGENTS.md。应让助手能够读取完整 Skill 包,才能查看索引背后的解释与代码示例。

它会直接给出项目的性能分数吗?

这套资料提供规则和示例,并没有测量你的项目。需要使用自己的构建与诊断工具,在一致条件下比较修改前后的结果。

安装这个 Skill

通过 Skills CLI,从已验证的 GitHub 来源添加这个 Skill。

npx skills add https://github.com/vercel-labs/agent-skills --skill vercel-react-best-practices

来源信息

仓库原始内容

按源仓库内容呈现,保留标题、案例、代码、表格、链接以及原文引用的演示图片。

Vercel React Best Practices

Comprehensive performance optimization guide for React and Next.js applications, maintained by Vercel. Contains 70 rules across 8 categories, prioritized by impact to guide automated refactoring and code generation.

When to Apply

Reference these guidelines when:

  • Writing new React components or Next.js pages
  • Implementing data fetching (client or server-side)
  • Reviewing code for performance issues
  • Refactoring existing React/Next.js code
  • Optimizing bundle size or load times

Rule Categories by Priority

PriorityCategoryImpactPrefix
1Eliminating WaterfallsCRITICALasync-
2Bundle Size OptimizationCRITICALbundle-
3Server-Side PerformanceHIGHserver-
4Client-Side Data FetchingMEDIUM-HIGHclient-
5Re-render OptimizationMEDIUMrerender-
6Rendering PerformanceMEDIUMrendering-
7JavaScript PerformanceLOW-MEDIUMjs-
8Advanced PatternsLOWadvanced-

Quick Reference

1. Eliminating Waterfalls (CRITICAL)

  • async-cheap-condition-before-await - Check cheap sync conditions before awaiting flags or remote values
  • async-defer-await - Move await into branches where actually used
  • async-parallel - Use Promise.all() for independent operations
  • async-dependencies - Use better-all for partial dependencies
  • async-api-routes - Start promises early, await late in API routes
  • async-suspense-boundaries - Use Suspense to stream content

2. Bundle Size Optimization (CRITICAL)

  • bundle-barrel-imports - Import directly, avoid barrel files
  • bundle-analyzable-paths - Prefer statically analyzable import and file-system paths to avoid broad bundles and traces
  • bundle-dynamic-imports - Use next/dynamic for heavy components
  • bundle-defer-third-party - Load analytics/logging after hydration
  • bundle-conditional - Load modules only when feature is activated
  • bundle-preload - Preload on hover/focus for perceived speed

3. Server-Side Performance (HIGH)

  • server-auth-actions - Authenticate server actions like API routes
  • server-cache-react - Use React.cache() for per-request deduplication
  • server-cache-lru - Use LRU cache for cross-request caching
  • server-dedup-props - Avoid duplicate serialization in RSC props
  • server-hoist-static-io - Hoist static I/O (fonts, logos) to module level
  • server-no-shared-module-state - Avoid module-level mutable request state in RSC/SSR
  • server-serialization - Minimize data passed to client components
  • server-parallel-fetching - Restructure components to parallelize fetches
  • server-parallel-nested-fetching - Chain nested fetches per item in Promise.all
  • server-after-nonblocking - Use after() for non-blocking operations

4. Client-Side Data Fetching (MEDIUM-HIGH)

  • client-swr-dedup - Use SWR for automatic request deduplication
  • client-event-listeners - Deduplicate global event listeners
  • client-passive-event-listeners - Use passive listeners for scroll
  • client-localstorage-schema - Version and minimize localStorage data

5. Re-render Optimization (MEDIUM)

  • rerender-defer-reads - Don't subscribe to state only used in callbacks
  • rerender-memo - Extract expensive work into memoized components
  • rerender-memo-with-default-value - Hoist default non-primitive props
  • rerender-dependencies - Use primitive dependencies in effects
  • rerender-derived-state - Subscribe to derived booleans, not raw values
  • rerender-derived-state-no-effect - Derive state during render, not effects
  • rerender-functional-setstate - Use functional setState for stable callbacks
  • rerender-lazy-state-init - Pass function to useState for expensive values
  • rerender-simple-expression-in-memo - Avoid memo for simple primitives
  • rerender-split-combined-hooks - Split hooks with independent dependencies
  • rerender-move-effect-to-event - Put interaction logic in event handlers
  • rerender-transitions - Use startTransition for non-urgent updates
  • rerender-use-deferred-value - Defer expensive renders to keep input responsive
  • rerender-use-ref-transient-values - Use refs for transient frequent values
  • rerender-no-inline-components - Don't define components inside components

6. Rendering Performance (MEDIUM)

  • rendering-animate-svg-wrapper - Animate div wrapper, not SVG element
  • rendering-content-visibility - Use content-visibility for long lists
  • rendering-hoist-jsx - Extract static JSX outside components
  • rendering-svg-precision - Reduce SVG coordinate precision
  • rendering-hydration-no-flicker - Use inline script for client-only data
  • rendering-hydration-suppress-warning - Suppress expected mismatches
  • rendering-activity - Use Activity component for show/hide
  • rendering-conditional-render - Use ternary, not && for conditionals
  • rendering-usetransition-loading - Prefer useTransition for loading state
  • rendering-resource-hints - Use React DOM resource hints for preloading
  • rendering-script-defer-async - Use defer or async on script tags

7. JavaScript Performance (LOW-MEDIUM)

  • js-batch-dom-css - Group CSS changes via classes or cssText
  • js-index-maps - Build Map for repeated lookups
  • js-cache-property-access - Cache object properties in loops
  • js-cache-function-results - Cache function results in module-level Map
  • js-cache-storage - Cache localStorage/sessionStorage reads
  • js-combine-iterations - Combine multiple filter/map into one loop
  • js-length-check-first - Check array length before expensive comparison
  • js-early-exit - Return early from functions
  • js-hoist-regexp - Hoist RegExp creation outside loops
  • js-min-max-loop - Use loop for min/max instead of sort
  • js-set-map-lookups - Use Set/Map for O(1) lookups
  • js-tosorted-immutable - Use toSorted() for immutability
  • js-flatmap-filter - Use flatMap to map and filter in one pass
  • js-request-idle-callback - Defer non-critical work to browser idle time

8. Advanced Patterns (LOW)

  • advanced-effect-event-deps - Don't put useEffectEvent results in effect deps
  • advanced-event-handler-refs - Store event handlers in refs
  • advanced-init-once - Initialize app once per app load
  • advanced-use-latest - useLatest for stable callback refs

How to Use

Read individual rule files for detailed explanations and code examples:

rules/async-parallel.md
rules/bundle-barrel-imports.md

Each rule file contains:

  • Brief explanation of why it matters
  • Incorrect code example with explanation
  • Correct code example with explanation
  • Additional context and references

Full Compiled Document

For the complete guide with all rules expanded: AGENTS.md

按任务相关推荐

来自同一仓库

更多 Skills

全部 Skills
vercel-labs
官方

web-design-guidelines

Review UI code for Web Interface Guidelines compliance. Use when asked to "review my UI", "check accessibility", "audit design", "review UX", or "check my site against best practices".

累计安装量
88.9万
GitHub Stars
3.5万
最近更新
11月20日
vercel-labs
官方

vercel-composition-patterns

React composition patterns that scale. Use when refactoring components with boolean prop proliferation, building flexible component libraries, or designing reusable APIs. Triggers on tasks involving compound components, render props, context providers, or component architecture. Includes React 19 API changes.

累计安装量
46.6万
GitHub Stars
3.5万
最近更新
11月20日
vercel-labs
官方

vercel-react-native-skills

React Native and Expo best practices for building performant mobile apps. Use when building React Native components, optimizing list performance, implementing animations, or working with native modules. Triggers on tasks involving React Native, Expo, mobile performance, or native platform APIs.

累计安装量
28.5万
GitHub Stars
3.5万
最近更新
11月20日
vercel-labs
官方

vercel-react-view-transitions

Guide for implementing smooth, native-feeling animations using React's View Transition API ( component, addTransitionType, and CSS view transition pseudo-elements). Use this skill whenever the user wants to add page transitions, animate route changes, create shared element animations, animate enter/exit of components, animate list reorder, implement directional (forward/back) navigation animations, or integrate view transitions in Next.js. Also use when the user mentions view transitions, startV

累计安装量
19.9万
GitHub Stars
3.5万
最近更新
11月20日