Course contentsShow
← Web Development
Lesson 1374 of 2,98225. Build Tools and BundlersFree lesson

Hot Module Replacement (HMR) in Vite

Understanding how Vite's HMR updates modules in the browser without full page reloads.

What You'll Learn

You'll learn how Vite's Hot Module Replacement (HMR) updates your code in the browser instantly without losing application state or requiring a full page reload.

What Is Hot Module Replacement?

Hot Module Replacement is Vite's superpower for development speed. When you save a file, HMR surgically replaces just the changed module in your running application—like swapping out a single LEGO brick in a completed structure without rebuilding the entire thing.

Without HMR, every code change would trigger a full page reload, losing your form inputs, scroll position, and application state. HMR preserves all of that while applying your updates.

How HMR Works in Vite

When Vite's development server detects a file change:

  1. Detects the change through its built-in file watcher
  2. Identifies the dependency chain to determine what modules are affected
  3. Sends an update signal to the browser over a WebSocket connection
  4. Replaces only the changed modules while preserving state

For CSS files, this is seamless—style updates appear instantly. For JavaScript modules, Vite attempts to "hot reload" the module while maintaining connections to the rest of your application.

The Developer Experience

Working with HMR feels magical. You edit a component's styles, hit save, and see the changes reflect immediately—no flash, no reload, no lost state. Your form stays filled, your modal stays open, your authentication remains intact.

This tight feedback loop dramatically speeds up development, especially when working on specific UI states that would be tedious to recreate after each page reload.

Key Takeaway: Vite's HMR updates only the changed modules in your browser without full page reloads, preserving application state and delivering instant visual feedback during development.