Glean 拾遗
Daily /2026-05-29 / Introducing React Best Practices: A Structured Repo for AI Agents

Introducing React Best Practices: A Structured Repo for AI Agents

Source vercel.com Glean’d 2026-05-29 09:10 Read 6 min
AI summary

Vercel distills 10+ years of React and Next.js optimization into a structured repo with 40+ rules across 8 categories, ordered by impact from eliminating waterfalls to JavaScript micro-optimizations. Each rule includes code samples and impact ratings, and compiles into an AGENTS.md document consumable by AI coding agents.

Original · 6 min
vercel.com ↗
§ 1

We've encapsulated 10+ years of React and Next.js optimization knowledge into react-best-practices, a structured repository optimized for AI agents and LLMs.

我们将十年以上的 React 和 Next.js 优化经验凝练为 react-best-practices,一个专为 AI 代理和 LLM 优化的结构化仓库。

§ 2

React performance work is usually, well, reactive. A release goes out, the app feels slower, and the team starts chasing symptoms. That’s expensive, and it’s easy to optimize the wrong thing.

We’ve seen the same root causes across production codebases for more than a decade:

Async work that accidentally becomes sequential

Large client bundles that grow over time

Components that re-render more than they need to

The “why” here is simple: these aren’t micro-optimizations. They show up as waiting time, jank, and repeat costs that hit every user session.

So, we put together this React best practices framework to make those problems easier to spot and faster to fix.

React 性能工作通常是被动的。版本发布后,应用感觉变慢了,团队才开始四处救火,不仅代价高昂,还容易盯错优化目标。

过去十多年,我们在生产代码中反复看到同样的根源问题:

  • 异步操作意外地串行执行
  • 客户端包体积随时间增长
  • 组件不必要地重复渲染

原因很简单:这些都不是微优化。它们表现为等待时间、卡顿和每次用户会话都重复付出的代价。

因此,我们整理了这个 React 最佳实践框架,让这些问题更容易发现,也能更快修复。

§ 3

Link to headingThe core idea: ordering

Most performance work fails because it starts too low in the stack.

If a request waterfall adds 600ms of waiting time, it doesn’t matter how optimized your useMemo calls are. If you ship an extra 300KB of JavaScript on every page, shaving a few microseconds off a loop won’t matter.

Performance work also compounds. A small regression you ship today becomes a long-term tax on every session until someone pays down the debt.

So the framework starts with the two fixes that usually move real-world metrics first:

Eliminate waterfalls

Reduce bundle size

Then it moves on to server-side performance, client-side fetching, and re-render optimization.

It includes 40+ rules across 8 categories, ordered by impact, from CRITICAL (eliminating waterfalls, reducing bundle size) to incremental (advanced patterns).‍​‌ ‌ ‌

Link to heading 核心理念:排序

大部分性能工作失败,是因为着手层次太低。

如果请求瀑布带来 600ms 等待时间,那么 useMemo 优化得再好也无济于事;如果每个页面多加载 300KB 的 JavaScript,在一个循环上节省几微秒也于事无补。

性能工作还有叠加效应:今天引入的一次小退化,会成为每次用户会话的长期负担,直到有人偿还这笔技术债务。

因此框架从两种最能移动真实性能指标的修复入手:

  • 消除请求瀑布
  • 减小包体积

接着再进入服务端性能、客户端数据获取和重复渲染优化。

整个框架包含 40 余条规则,分为 8 个类别,按影响力排序,从关键(消除瀑布、减小包体积)到渐进式(高级模式)。

§ 4

Link to headingWhat else is inside?

The repository covers eight performance categories:

Eliminating async waterfalls

Bundle size optimization

Server-side performance

Client-side data fetching

Re-render optimization

Rendering performance

Advanced patterns

JavaScript performance

Each rule includes an impact rating (CRITICAL to LOW) to help prioritize fixes, plus code examples showing what breaks and how to fix it.

For example, here’s a common pattern that blocks unused code:

Incorrect (blocks both branches):

async function handleRequest(userId: string, skipProcessing: boolean) {
  const userData = await fetchUserData(userId)
 
  if (skipProcessing) {
    // Returns immediately but still waited for userData
    return { skipped: true }
  }
  // Only this branch uses userData
  return processUserData(userData)
}

Correct (only blocks when needed):

async function handleRequest(
  userId: string,
  skipProcessing: boolean
) {
  if (skipProcessing) {
    return { skipped: true }
  }
 
  const userData = await fetchUserData(userId)
  return processUserData(userData)
}

Individual rule files compile into AGENTS.md, a single document that your agents can query when reviewing code or suggesting optimizations. It’s designed to be followed consistently, including by AI agents doing refactors, so teams can apply the same decisions across a large codebase.

Link to heading 仓库里还有什么?

该仓库覆盖八个性能类别:

  • 消除异步瀑布
  • 包体积优化
  • 服务端性能
  • 客户端数据获取
  • 重复渲染优化
  • 渲染性能
  • 高级模式
  • JavaScript 性能

每条规则都带有影响力评级(从 CRITICAL 到 LOW),帮助排定修复优先级,并附有代码示例,展示问题场景与修复方式。

例如,一个常见的阻塞无用代码的模式:

错误写法(阻塞两个分支):

async function handleRequest(userId: string, skipProcessing: boolean) {
  const userData = await fetchUserData(userId)
 
  if (skipProcessing) {
    // 虽然立即返回,但仍需等待 userData
    return { skipped: true }
  }
  // 只有这个分支才真正用到 userData
  return processUserData(userData)
}

正确写法(只在必要时阻塞):

async function handleRequest(
  userId: string,
  skipProcessing: boolean
) {
  if (skipProcessing) {
    return { skipped: true }
  }
 
  const userData = await fetchUserData(userId)
  return processUserData(userData)
}

每条规则的独立文件会编译为 AGENTS.md,一份可供代理在审查代码或建议优化时查询的统一文档。它设计成可被持续遵循,包括 AI 代理进行重构时,这样团队就能在整个代码库中应用一致的决策。

§ 5

Link to headingHow these practices were collected

These aren’t theoretical. They come from real performance work on production codebases.

A few examples:

Combining loop iterations

A chat page was scanning the same list of messages eight separate times. We combined it into a single pass, which adds up when you have thousands of messages.

Parallelizing awaits

An API was waiting for one database call to finish before starting the next, even though they didn’t depend on each other. Running them at the same time cut the total wait in half.

Lazy State Initialization

A component was parsing a JSON config from localStorage on every render, even though it only needed it once for state initialization. Wrapping the it in a callback (useState(() => JSON.parse(...))) eliminated wasted work.

Link to heading 这些实践是如何收集的

这些不是理论推演,而是来自生产环境的真实性能工作。

几个例子:

合并循环迭代

一个聊天页面曾对同一消息列表遍历了八次。我们将其合并为一次遍历,当消息数量成千上万时,效果显著。

并行化 await

一个 API 在数据库调用之间依次等待,哪怕它们互不依赖。同时执行将总等待时间缩短一半。

惰性状态初始化

一个组件每次渲染都从 localStorage 解析一份 JSON 配置,实际上只需在状态初始化时读取一次。用回调包装(useState(() => JSON.parse(...)))避免无谓重复。

§ 6

Link to headingUsing react-best-practices in your coding agent

These best practices are also packaged up as Agent Skills that install into Opencode, Codex, Claude Code, Cursor, and other coding agents. When your agent spots cascading useEffect calls or heavy client-side imports, it can reference these patterns and suggest fixes.

npx skills add vercel-labs/agent-skills

Check out the react-best-practices repository.

Link to heading 在编码代理中使用 react-best-practices

这些最佳实践也打包为代理技能(Agent Skills),可安装到 Opencode、Codex、Claude Code、Cursor 等编码代理中。当代理发现级联的 useEffect 调用或过重的客户端导入时,便可引用这些模式并提出修复建议。

npx skills add vercel-labs/agent-skills

查看 react-best-practices 仓库。

Open source ↗