Hot Module Replacement
Problem
Every code change triggers a full page reload, losing application state and forcing developers to manually recreate their working context. Form inputs reset, modals close, and navigation state disappears with each edit. Development velocity suffers as developers waste time clicking through the same flows repeatedly. Multi-step workflows become tedious to test during active development.
Solution
Update modules in a running application without full page reload, preserving state and speeding development. This provides instant feedback as you code without losing your place in the application.
Example
This example demonstrates configuring Hot Module Replacement in Vite to update code in the browser without full page reloads.
// vite.config.js - Enable HMR in development server
export default {
server: {
hmr: true // Enable hot module replacement
}
};
// In your module - opt into HMR to preserve state across updates
if (import.meta.hot) {
// Accept updates to this module without full reload
import.meta.hot.accept();
}
Benefits
- Dramatically speeds up development by preserving application state between code changes.
- Provides instant feedback as you type without waiting for full page reloads.
- Reduces context switching by keeping your place in multi-step workflows.
- Improves developer experience by making iteration feel instantaneous.
Tradeoffs
- Requires build tool configuration and may not work with all module types.
- Can lead to stale state if modules don’t properly handle updates.
- Debugging is harder when state persists across code changes unexpectedly.
- May hide bugs that only appear after full reloads or in production builds.