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

Open
#1,565 2 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
45/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
react, typescript, vite

Research direction

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.

Written by the indexing model from the issue text.

Description

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
Dominant language
TypeScript
Stars
1.2k
Forks
287
Avg merge
1d 17h
Merged PRs (30d)
19

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from vitejs/vite-plugin-react

All issues in vitejs/vite-plugin-react

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.