Official编程与开发github.com

Vercel React 最佳实践

来自 Vercel 工程团队的 React 和 Next.js 性能优化指南。在编写、审查或重构 React/Next.js 代码时使用此技能,以确保采用最佳性能模式。适用于涉及 React 组件、Next.js 页面、数据获取、打包优化或性能改进的任务。

Vercel React 最佳实践 是什么?

该技能提供了一份全面的性能优化指南,专为 React 和 Next.js 应用设计,由 Vercel 维护。它包含 70 条规则,分为 8 个类别,并按影响优先级排序,旨在指导自动化的代码重构和生成。主要规则类别包括:消除瀑布式请求(关键)、打包大小优化(关键)、服务端性能(高)、客户端数据获取(中高)、重渲染优化(中)、渲染性能(中)、JavaScript 性能(低中)和高级模式(低)。每条规则都有详细的解释、不正确的代码示例及说明、正确的代码示例及说明,以及额外的上下文和参考资料。该技能适用于编写新组件、实现数据获取、审查代码性能问题、重构现有代码或优化打包大小和加载时间等场景。它支持 Claude Code、Cursor 和 Codex 等 AI 代理平台,并可通过规则前缀(如 `async-`、`bundle-`)快速引用具体规则。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/vercel-labs/agent-skills/tree/main/skills/react-best-practices

Installed? Explore more 编程与开发 skills: steipete/bluebubbles, steipete/eightctl, steipete/blucli · View all 6 →

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

Vercel React 最佳实践 是做什么的?

由 Vercel 维护的 React 和 Next.js 应用综合性能优化指南。包含 70 条规则,分为 8 个类别,按影响优先级排序,以指导自动化的代码重构和生成。

何时应用

在以下情况参考这些指南:

  • 编写新的 React 组件或 Next.js 页面
  • 实现数据获取(客户端或服务端)
  • 审查代码中的性能问题
  • 重构现有的 React/Next.js 代码
  • 优化打包大小或加载时间

按优先级排列的规则类别

优先级类别影响前缀
1消除瀑布式请求关键async-
2打包大小优化关键bundle-
3服务端性能高server-
4客户端数据获取中高client-
5重渲染优化中rerender-
6渲染性能中rendering-
7JavaScript 性能低中js-
8高级模式低advanced-

快速参考

1. 消除瀑布式请求 (关键)

  • async-cheap-condition-before-await - 在等待标志或远程值之前,先检查廉价的同步条件
  • async-defer-await - 将 await 移动到实际使用它的分支中
  • async-parallel - 对独立操作使用 Promise.all()
  • async-dependencies - 对部分依赖使用 better-all
  • async-api-routes - 在 API 路由中尽早启动 promise,延迟 await
  • async-suspense-boundaries - 使用 Suspense 来流式传输内容

2. 打包大小优化 (关键)

  • bundle-barrel-imports - 直接导入,避免使用 barrel 文件
  • bundle-analyzable-paths - 优先使用静态可分析的导入和文件系统路径,以避免过大的打包和跟踪
  • bundle-dynamic-imports - 对重型组件使用 next/dynamic
  • bundle-defer-third-party - 在 hydration 后加载分析/日志记录
  • bundle-conditional - 仅在功能激活时加载模块
  • bundle-preload - 在悬停/聚焦时预加载以提高感知速度

3. 服务端性能 (高)

  • server-auth-actions - 像 API 路由一样对服务端操作进行身份验证
  • server-cache-react - 使用 React.cache() 进行每个请求的去重
  • server-cache-lru - 使用 LRU 缓存进行跨请求缓存
  • server-dedup-props - 避免在 RSC props 中重复序列化
  • server-hoist-static-io - 将静态 I/O(字体、标志)提升到模块级别
  • server-no-shared-module-state - 避免在 RSC/SSR 中使用模块级别的可变请求状态
  • server-serialization - 最小化传递给客户端组件的数据
  • server-parallel-fetching - 重构组件以并行化获取
  • server-parallel-nested-fetching - 在 Promise.all 中为每个项目链式嵌套获取
  • server-after-nonblocking - 使用 after() 进行非阻塞操作

4. 客户端数据获取 (中高)

  • client-swr-dedup - 使用 SWR 进行自动请求去重
  • client-event-listeners - 去重全局事件监听器
  • client-passive-event-listeners - 对滚动使用被动监听器
  • client-localstorage-schema - 版本化并最小化 localStorage 数据

5. 重渲染优化 (中)

  • rerender-defer-reads - 不要订阅仅在回调中使用的状态
  • rerender-memo - 将耗时的操作提取到 memo 化的组件中
  • rerender-memo-with-default-value - 提升默认的非原始 props
  • rerender-dependencies - 在 effect 中使用原始依赖
  • rerender-derived-state - 订阅派生布尔值,而不是原始值
  • rerender-derived-state-no-effect - 在渲染期间派生状态,而不是在 effect 中
  • rerender-functional-setstate - 使用函数式 setState 获取稳定的回调
  • rerender-lazy-state-init - 对昂贵的初始值,将函数传递给 useState
  • rerender-simple-expression-in-memo - 避免对简单原始值使用 memo
  • rerender-split-combined-hooks - 拆分具有独立依赖的 hooks
  • rerender-move-effect-to-event - 将交互逻辑放在事件处理程序中
  • rerender-transitions - 对非紧急更新使用 startTransition
  • rerender-use-deferred-value - 延迟昂贵的渲染以保持输入响应
  • rerender-use-ref-transient-values - 对瞬态频繁值使用 refs
  • rerender-no-inline-components - 不要在组件内部定义组件

6. 渲染性能 (中)

  • rendering-animate-svg-wrapper - 动画化 div 包装器,而不是 SVG 元素
  • rendering-content-visibility - 对长列表使用 content-visibility
  • rendering-hoist-jsx - 将静态 JSX 提取到组件外部
  • rendering-svg-precision - 降低 SVG 坐标精度
  • rendering-hydration-no-flicker - 对客户端专用数据使用内联脚本
  • rendering-hydration-suppress-warning - 抑制预期的不匹配
  • rendering-activity - 使用 Activity 组件进行显示/隐藏
  • rendering-conditional-render - 使用三元表达式,而不是 && 进行条件渲染
  • rendering-usetransition-loading - 优先使用 useTransition 处理加载状态
  • rendering-resource-hints - 使用 React DOM 资源提示进行预加载
  • rendering-script-defer-async - 在 script 标签上使用 defer 或 async

7. JavaScript 性能 (低中)

  • js-batch-dom-css - 通过类或 cssText 分组 CSS 更改
  • js-index-maps - 为重复查找构建 Map
  • js-cache-property-access - 在循环中缓存对象属性
  • js-cache-function-results - 在模块级 Map 中缓存函数结果
  • js-cache-storage - 缓存 localStorage/sessionStorage 读取
  • js-combine-iterations - 将多个 filter/map 合并为一个循环
  • js-length-check-first - 在昂贵的比较之前检查数组长度
  • js-early-exit - 从函数中提前返回
  • js-hoist-regexp - 将 RegExp 创建提升到循环外部
  • js-min-max-loop - 使用循环查找最小值/最大值,而不是排序
  • js-set-map-lookups - 使用 Set/Map 进行 O(1) 查找
  • js-tosorted-immutable - 使用 toSorted() 实现不可变性
  • js-flatmap-filter - 使用 flatMap 在一次遍历中映射和过滤
  • js-request-idle-callback - 将非关键工作推迟到浏览器空闲时间

8. 高级模式 (低)

  • advanced-effect-event-deps - 不要将 useEffectEvent 的结果放在 effect 依赖中
  • advanced-event-handler-refs - 将事件处理程序存储在 refs 中
  • advanced-init-once - 每次应用加载仅初始化一次
  • advanced-use-latest - 使用 useLatest 获取稳定的回调 refs

如何使用

阅读单个规则文件以获取详细的解释和代码示例:

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

每个规则文件包含:

  • 问题重要性的简要说明
  • 不正确的代码示例及说明
  • 正确的代码示例及说明
  • 额外的上下文和参考资料

完整编译文档

获取包含所有规则的完整指南:AGENTS.md

Individual skills in this repo

This repo contains 11 individual skills — each has its own dedicated page.

deploy-to-vercel

Deploy applications and websites to Vercel. Use when the user requests deployment actions like "deploy my app", "deploy and give me the link", "push this live", or "create a preview deployment".

vercel-cli-with-tokens

Deploy and manage projects on Vercel using token-based authentication. Use when working with Vercel CLI using access tokens rather than interactive login — e.g.

vercel-composition-patterns

React composition patterns that scale. Use when refactoring components with

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.

vercel-labs/vercel-react-best-practices

React and Next.js performance optimization guidelines from Vercel Engineering. This skill should be used when writing, reviewing, or refactoring React/Next.js code to ensure optimal performance patterns. Triggers on tasks involving React components, Next.js pages, data fetching, bundle optimization, or performance improvements.

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.

vercel-labs/vercel-react-view-transitions

Guide for implementing smooth, native-feeling animations using React's View Transition API (`<ViewTransition>` 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, `startViewTransition`, `ViewTransition`, transition types, or asks about animating between UI states in React without third-party animation libraries.

vercel-optimize

Use for Vercel cost and performance optimization on deployed projects, especially Next.js, SvelteKit, Nuxt, and limited Astro apps. Collect Vercel metrics, usage, project config, and code scan results first; investigate only metric-backed candidates; produce ranked recommendations grounded in verified files and version-aware Vercel/framework docs. Trigger for Vercel bill reduction, slow or expensive routes, caching opportunities, Function Invocations, Build Minutes, Fast Data Transfer, Core Web Vitals, Bot Management, Fluid compute, or cost breakdown requests.

vercel-react-native-skills

React Native and Expo best practices for building performant mobile apps. Use

web-design-guidelines

Review UI code for Web Interface Guidelines compliance. Use when asked to

writing-guidelines

Review docs/prose for Writing Guidelines compliance. Use when asked to

相关技能