Skip to main content

Hot Module Replacement

Hot Module Replacement (HMR) enables you to see changes instantly during development without losing application state.

How HMR Works

When you edit a file:
  1. Vite detects the change
  2. Sends update to browser via WebSocket
  3. React Router applies changes
  4. UI updates without full page reload
  5. Application state is preserved

Framework Mode

HMR is automatic in framework mode:
Edit any route file and see instant updates:

What Gets Updated

Components

Component changes apply immediately:

Loaders

Loader changes trigger revalidation:

Actions

Action changes are applied on next submission:

Styles

CSS updates without reload:

State Preservation

React Fast Refresh preserves component state:

State Reset Cases

State resets when:
  1. Exporting non-components:
  1. Anonymous exports:
  1. Class components:

Route Updates

Route configuration updates automatically:

Loader Revalidation

Control when loaders revalidate:

Custom HMR Handling

Manual HMR logic for special cases:

Data Mode

Enable HMR in data mode:

Production Builds

HMR code is removed in production:
No performance impact on production builds.

Debugging HMR

Enable HMR Logging

Check HMR Status

Force Full Reload

Some changes require full reload:

HMR Boundaries

Define update boundaries:

Common Issues

HMR Not Working

Problem: Changes don’t update Solutions:
  1. Check dev server is running
  2. Verify WebSocket connection
  3. Clear browser cache
  4. Restart dev server

Duplicate Module Errors

Problem: “Module imported multiple times” Solution: Use consistent import paths

State Loss

Problem: State resets on every change Solution: Export named functions

Custom Server HMR

Enable HMR with custom servers:

Best Practices

  1. Use named exports: For better Fast Refresh
  2. Avoid side effects: In module scope
  3. Split large files: Improves HMR speed
  4. Use HMR for dev only: Don’t rely on it in production
  5. Test full reloads: Ensure app works without HMR

Performance Tips

Optimize HMR Speed

Reduce Update Scope

Framework Mode HMR

Framework mode includes enhanced HMR:
  • Route-level updates
  • Loader revalidation
  • Asset updates
  • CSS hot reload
  • React Fast Refresh
All enabled by default with pnpm dev.