This is the client HMR API. For handling HMR update in plugins, see handleHotUpdate.

The manual HMR API is primarily intended for framework and tooling authors. As an end user, HMR is likely already handled for you in the framework specific starter templates.

Vite exposes its manual HMR API via the special object:

interface ImportMeta {
  readonly hot?: ViteHotContext

interface ViteHotContext {
  readonly data: any

  accept(): void
  accept(cb: (mod: any) => void): void
  accept(dep: string, cb: (mod: any) => void): void
  accept(deps: readonly string[], cb: (mods: any[]) => void): void

  dispose(cb: (data: any) => void): void
  decline(): void
  invalidate(): void

  // `InferCustomEventPayload` provides types for built-in Vite events
  on<T extends string>(
    event: T,
    cb: (payload: InferCustomEventPayload<T>) => void
  ): void
  send<T extends string>(event: T, data?: InferCustomEventPayload<T>): void

Required Conditional Guard

First of all, make sure to guard all HMR API usage with a conditional block so that the code can be tree-shaken in production:

if ( {
  // HMR code


For a module to self-accept, use with a callback which receives the updated module:

export const count = 1

if ( { => {
    console.log('updated: count is now ', newModule.count)

A module that "accepts" hot updates is considered an HMR boundary.

Note that Vite's HMR does not actually swap the originally imported module: if an HMR boundary module re-exports imports from a dep, then it is responsible for updating those re-exports (and these exports must be using let). In addition, importers up the chain from the boundary module will not be notified of the change.

This simplified HMR implementation is sufficient for most dev use cases, while allowing us to skip the expensive work of generating proxy modules.

hot.accept(deps, cb)

A module can also accept updates from direct dependencies without reloading itself:

import { foo } from './foo.js'


if ( {'./foo.js', (newFoo) => {
    // the callback receives the updated './foo.js' module

  // Can also accept an array of dep modules:
    ['./foo.js', './bar.js'],
    ([newFooModule, newBarModule]) => {
      // the callback receives the updated modules in an Array


A self-accepting module or a module that expects to be accepted by others can use hot.dispose to clean-up any persistent side effects created by its updated copy:

function setupSideEffect() {}


if ( { => {
    // cleanup side effect

The object is persisted across different instances of the same updated module. It can be used to pass on information from a previous version of the module to the next one.


Calling indicates this module is not hot-updatable, and the browser should perform a full reload if this module is encountered while propagating HMR updates.


For now, calling simply reloads the page.

hot.on(event, cb)

Listen to an HMR event.

The following HMR events are dispatched by Vite automatically:

  • 'vite:beforeUpdate' when an update is about to be applied (e.g. a module will be replaced)
  • 'vite:beforeFullReload' when a full reload is about to occur
  • 'vite:beforePrune' when modules that are no longer needed are about to be pruned
  • 'vite:error' when an error occurs (e.g. syntax error)

Custom HMR events can also be sent from plugins. See handleHotUpdate for more details.

hot.send(event, data)

Send custom events back to Vite's dev server.

If called before connected, the data will be buffered and sent once the connection is established.

See Client-server Communication for more details.