nextRound
TechnologiesCoding ProblemsBookmarksLearning PathsLogin
nextRound
TechnologiesCoding ProblemsBookmarksLearning PathsLogin
Questions
1 of 6
1How Vite's HMR works — WebSocket-based update propagation
2HMR API — hot.accept(), hot.dispose(), hot.invalidate()
3What are the difference between full page reload and HMR update?
4How Vite handles HMR for CSS vs JavaScript vs frameworks
5How framework-specific HMR works (React Fast Refresh, Vue HMR)
6What is hot.data?
ViteVite
Basics
Hot Module Replacement
Configuration
Plugin System
Asset Handling
CSS Handling
Environment Variables
Code Splitting
Build Optimization and Performance
SSR
Library Mode
Monorepo and Workspace Support
Testing
Production Deployment
Advanced Topics
01 / 06
nextRound

AI-powered interview preparation platform. Practice with curated questions, mock interviews, and personalized learning paths to crack your dream tech interview.

Quick Links

  • Technologies
  • Mock Interviews
  • Saved Questions
  • Pricing

Company

  • About Us
  • Contact Us

Legal

  • Privacy Policy
  • Terms of Use

© 2026 nextRound. All rights reserved.

How Vite's HMR works — WebSocket-based update propagation

Vite's HMR uses a WebSocket connection between dev server and browser to propagate precise module updates, with the server determining affected modules via its module graph and the client dynamically importing updates while preserving application state

Vite's Hot Module Replacement (HMR) system is a cornerstone of its exceptional developer experience, enabling near-instant updates when code changes without full page reloads . Unlike traditional bundlers that rebuild large portions of the application, Vite leverages native ES modules and a sophisticated WebSocket-based communication layer to achieve precise, fast updates that preserve application state.

At its core, Vite's HMR consists of server-side and client-side components connected via a persistent WebSocket. This channel enables real-time, bidirectional communication . The server broadcasts update notifications, while the client can send custom events for debugging or tooling purposes . The system is designed with security in mind, using cryptographically random tokens to prevent unauthorized WebSocket connections .

Server-Side HMR Workflow
  1. 1

    File detection: Vite's file watcher monitors source files; when a change occurs, the handleHMRUpdate function determines the file type and appropriate response .

  2. 2

    Module graph analysis: Vite maintains an EnvironmentModuleGraph tracking dependencies. The propagateUpdate function walks the import chain to find HMR boundaries—modules that have accepted updates .

  3. 3

    Boundary detection: Using a recursive algorithm, the server identifies which modules are affected and whether they can be hot-updated or require a full reload for circular imports .

  4. 4

    Payload preparation: The server constructs an update payload containing module paths and timestamps, then broadcasts via WebSocket .

Server-Side HMR Payload Structure

The browser-side HMR client, injected by Vite during development, maintains a WebSocket connection to the server . When it receives an update payload, it processes each update based on type—JavaScript updates trigger dynamic re-imports with timestamp query parameters, while CSS updates clone <link> elements to avoid FOUC .

Client-Side Dynamic Import for HMR

Vite exposes the HMR API to developers through the import.meta.hot object, which is only available during development . This API allows modules to declare their HMR acceptance and handle updates gracefully. For Vue or React components, framework integrations automatically handle this, but custom modules can implement their own HMR logic .

Custom HMR Acceptance Patterns

Plugins can integrate with HMR through the handleHotUpdate hook, which allows filtering or modifying affected modules . The system also supports custom WebSocket events—plugins can send and receive arbitrary messages for debugging or tooling purposes .

Plugin HMR Integration
Key Advantages Over Traditional Bundlers
  1. 1

    Speed: Vite's HMR updates typically complete in under 100ms, compared to Webpack's 800ms-1.2s for medium projects .

  2. 2

    Precision: The module graph allows pinpoint updates without rebuilding unrelated modules .

  3. 3

    State preservation: Modules can preserve state through HMR boundaries using custom accept handlers .

  4. 4

    Circular import handling: Vite 5.1+ allows HMR to apply even with circular dependencies, only reloading on actual errors .

  • https://vitejs.dev/guide/api-hmr.html
  • https://vitejs.dev/guide/features.html#hot-module-replacement
  • https://developer.mozilla.org/en-US/docs/Web/API/WebSocket