Vue Hot Reload API

2.3.4 · maintenance · verified Sun Apr 19

vue-hot-reload-api is a foundational utility designed to provide the core API for hot module replacement (HMR) within Vue 2.x applications. It serves as an internal dependency for higher-level build tools like `vue-loader` and `vueify`, enabling developers to update Vue components in the browser without a full page reload, thereby preserving application state during development. The package's current stable version is 2.3.4, with releases primarily focusing on compatibility fixes for the Vue 2.x ecosystem. It is critically important to note that this package is strictly tied to Vue 2.x; it offers no support for Vue 3 or later versions, which have their own integrated HMR mechanisms. Therefore, its utility is confined to legacy Vue 2 projects. Application developers generally do not interact with this package directly but rather benefit from its functionality through their chosen build setup. Its release cadence is infrequent, reflecting its mature status within a specific, older framework version.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to initialize the hot-reload API, install it with Vue, and implement hot reloading for a component's template (rerender) or full options (reload) within a Webpack HMR context.

const myComponentOptions = {
  data () { return { msg: 'Hello' } },
  created () { console.log('Component created!') },
  render (h) { return h('div', this.msg) }
}

// assuming Webpack's HMR API.
// https://webpack.js.org/guides/hot-module-replacement/
if (module.hot) {
  const api = require('vue-hot-reload-api')
  const Vue = require('vue')

  // make the API aware of the Vue that you are using.
  // also checks compatibility.
  api.install(Vue)

  // compatibility can be checked via api.compatible after installation
  if (!api.compatible) {
    throw new Error('vue-hot-reload-api is not compatible with the version of Vue you are using.')
  }

  // indicate this module can be hot-reloaded
  module.hot.accept()

  if (!module.hot.data) {
    // for each component option object to be hot-reloaded,
    // you need to create a record for it with a unique id.
    // do this once on startup.
    api.createRecord('very-unique-id', myComponentOptions)
  } else {
    // if a component has only its template or render function changed,
    // you can force a re-render for all its active instances without
    // destroying/re-creating them. This keeps all current app state intact.
    api.rerender('very-unique-id', myComponentOptions)

    // --- OR ---

    // if a component has non-template/render options changed,
    // it needs to be fully reloaded. This will destroy and re-create all its
    // active instances (and their children).
    api.reload('very-unique-id', myComponentOptions)
  }
}

view raw JSON →