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:- Vite detects the change
- Sends update to browser via WebSocket
- React Router applies changes
- UI updates without full page reload
- Application state is preserved
Framework Mode
HMR is automatic in framework mode: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:- Exporting non-components:
- Anonymous exports:
- 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: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:- Check dev server is running
- Verify WebSocket connection
- Clear browser cache
- Restart dev server
Duplicate Module Errors
Problem: “Module imported multiple times” Solution: Use consistent import pathsState Loss
Problem: State resets on every change Solution: Export named functionsCustom Server HMR
Enable HMR with custom servers:Best Practices
- Use named exports: For better Fast Refresh
- Avoid side effects: In module scope
- Split large files: Improves HMR speed
- Use HMR for dev only: Don’t rely on it in production
- 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
pnpm dev.