Run React Refresh at vite:afterUpdate instead of after a fixed 16 ms

未关闭
#1,565 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
45/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
活跃
技术栈
react, typescript, vite

调研方向

Start in packages/common/refresh-runtime.js, where enqueueUpdate is a debounce with a 16 ms delay that runs the hooks and then performReactRefresh. Look at how the client emits vite:afterUpdate and where validateRefreshBoundaryAndEnqueueUpdate is called. Done means flushing the pending refresh on vite:afterUpdate while the timer still covers enqueues outside an update. Check the timing in the existing refresh tests.

由索引模型根据 Issue 内容生成。

描述

Drafted with an AI assistant (Claude), reviewed by me.

Related plugins
  • plugin-react
Description

React Refresh runs 16 ms after the last accepted module (packages/common/refresh-runtime.js, 6.3.0):

const enqueueUpdate = debounce(async () => {
  if (hooks.length) await Promise.all(hooks.map((cb) => cb()))
  performReactRefresh()
}, 16)

So every Fast Refresh edit waits at least 16 ms after its modules have run.

We see it in TanStack Router, which waits for the pass through __registerBeforePerformReactRefresh before applying a route edit: an edit of only a route's loader went from 16.9 to 36.8 ms save-to-DOM when we started waiting (1,485-route app).

Suggested solution

By the time the client emits vite:afterUpdate, every module of the payload has run its accept callback, so every validateRefreshBoundaryAndEnqueueUpdate call of that update has been made. Flush there, and keep the timer for enqueues outside an update:

import.meta.hot.on('vite:afterUpdate', () => flushPendingRefresh())
Alternative

Keep the debounce but expose a way to flush it.

Validations
  • Follow our Code of Conduct
  • Read the Contributing Guidelines
  • Read the docs
  • Check that there isn't already an issue that request the same feature to avoid creating a duplicate
主要语言
TypeScript
星标
1.2k
派生
287
平均合并
1 天 17 小时
30 天内合并 PR
19

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

vitejs/vite-plugin-react 的其他 Issue

查看 vitejs/vite-plugin-react 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。