Skip to content

fix: Vite HMR broken when using reactDevtools() plugin#34

Merged
piotrski merged 3 commits into
mainfrom
fix/vite-hmr-broken
Mar 4, 2026
Merged

fix: Vite HMR broken when using reactDevtools() plugin#34
piotrski merged 3 commits into
mainfrom
fix/vite-hmr-broken

Conversation

@piotrski
Copy link
Copy Markdown
Collaborator

@piotrski piotrski commented Mar 4, 2026

Summary

  • Fixed Vite HMR (hot module replacement) breaking when reactDevtools() is added to vite.config.ts
  • The connect module now preserves the react-refresh runtime's inject wrapper when replacing the devtools hook stub, so both Fast Refresh and devtools inspection work correctly
  • Replaced the unnecessary top-level-await esbuild option with optimizeDeps.include for react-devtools-core to prevent Vite dep re-optimization at runtime

Root Cause

@vitejs/plugin-react wraps __REACT_DEVTOOLS_GLOBAL_HOOK__.inject with a react-refresh wrapper that captures scheduleRefresh from React's renderer. The connect module was deleting the hook and calling initialize(), which installed a new hook without the refresh wrapper — silently breaking Fast Refresh.

Test plan

  • Added e2e test that modifies a source file and verifies HMR applies the change without a full page reload
  • All 61 unit tests pass
  • All 6 browser e2e tests pass (including the new HMR test)
  • Manually verified with headed browser: file change reflected via HMR, window.__hmrMarker survives (no full reload)

Closes #33

🤖 Generated with Claude Code

piotrski and others added 3 commits March 4, 2026 23:48
The connect module was destroying the react-refresh runtime's inject
wrapper when it deleted __REACT_DEVTOOLS_GLOBAL_HOOK__ and called
initialize(). This caused Fast Refresh to lose its scheduleRefresh
reference, silently breaking HMR.

Fix by saving the old hook's inject before replacement and re-applying
it after initialize(), so both devtools and Fast Refresh share the hook.

Also replace the unnecessary top-level-await esbuild option with
optimizeDeps.include for react-devtools-core, preventing Vite from
triggering dep re-optimization at runtime.

Closes #33

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Verifies that file changes are hot-updated without a full page reload
when the reactDevtools() Vite plugin is active.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Mar 4, 2026

🦋 Changeset detected

Latest commit: 705c92c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
agent-react-devtools Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@piotrski piotrski merged commit 303f9e4 into main Mar 4, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

After adding reactDevtools() in vite.config.ts - hot reloading breaks

1 participant