Hot Module Replacement is Easy

This ad is not shown to multipass and full ticket holders
JSNation US
JSNation US 2025
November 17 - 20, 2025
New York, US & Online
See JS stars in the US biggest planetarium
Learn More
In partnership with Focus Reactive
Upcoming event
JSNation US 2025
JSNation US 2025
November 17 - 20, 2025. New York, US & Online
Learn more
Bookmark
Rate this content

Learn how Vite's Hot Module Replacement (HMR) works under the hood and all the key concepts that power it.

This talk has been presented at JSNation 2025, check out the latest edition of this JavaScript Conference.

FAQ

Tools like Vite, Parcel, and Webpack have built-in support for homework replacement, allowing developers to update code more efficiently.

HTML APIs in homework replacement act as hooks in the lifecycle, allowing you to accept changes, dispose of unneeded states, prune removed modules, and invalidate when changes are not as expected.

When a module accepts HMR, the hooks implemented for that module are executed, and any new modules are updated within the accepted boundary.

The server watches for file changes, triggers invalidation and propagation, and informs the client whether modules need to be updated or if a full page reload is required.

The client sets up HTML APIs to receive instructions from the server. It updates the necessary modules or performs a full reload based on the server's instructions.

HMR allows developers to see changes in real-time without refreshing the page, speeding up the development process by allowing faster iteration and testing.

For more in-depth details on HMR implementation in Vite, you can visit Vite's Discourse or follow discussions on platforms like BlueSky or X.

A full page reload is required if no modules can accept changes or if there are interconnected modules where not all can accept changes.

Homework replacement, also known as Hot Module Replacement (HMR), allows you to update your code without having to refresh your page. It enables immediate viewing of changes in your browser when updating components or adjusting styles.

Bjorn Lu
Bjorn Lu
11 min
16 Jun, 2025

Comments

Sign in or register to post your comment.
Video Summary and Transcription
Welcome to JS Nation. Homework replacement, also known as HTML, allows code updates without page refresh. Understanding HTML involves APIs, server reactions to file edits, and client-side handling of changes. HTML API lifecycle includes attaching callbacks, disposing unneeded states, and accepting new modules. Server handling file changes, finding related modules, and HTML propagation decisions determine module execution and page reload. Propagation scenarios dictate HTML updates within boundaries or full reloads. Client-server communication through WebSocket manages reloads or module updates.
Available in Español: Hot Module Replacement es Fácil

1. Homework Replacement Overview

Short description:

Welcome to JS Nation. Homework replacement, also known as HTML, allows code updates without page refresh. Tools like Vite, Parcel, Webpack have it built-in. Understanding HTML involves APIs, server reactions to file edits, and client-side handling of changes.

Hello. Welcome to JS Nation. I'm Bjorn. I want to talk about homework replacement today. So homework replacement is a big topic. Hopefully, this talk will make it more easy to understand for you and more digestible.

So let's jump right into it. What is homework replacement? Homework replacement is also known as HTML. It allows you to update your code without having to refresh your page. So when you update your components or adjust your styles, you can immediately see the changes in your browser. It allows you to iterate much faster than having to wait for the bundle to finish.

So if you have used tools like Vite, Parcel, Webpack, for example, in this right-side here, this Vite logo, and also on my shirt here, this Vite logo, it's where I'm a co-team of. So they have homework replacement built in. So if you have used them, you likely have already experienced something like this in practice. So let's jump to how it really works inside them. So there are different ways to understand HTML. It's quite a big system.

So there's kind of three things that we can look into from the point of view. There are HTML APIs, where you implement how the module replacement is actually done. There is the flow from the server, so when you edit the file, how the server reacts to it. There's also the flow from the client, so the browser will try to handle changes from the server. These three things we're going to cover next. So HTML APIs, you can think of it as hooks. In the HTML lifecycle, sometimes you would have to accept changes when a new module comes in. So this hook tells you that you can accept changes. You can do the replacement. So you'd attach a callback so that you do the necessary changes there. There's a disposal if you need to dispose any unneeded states.

2. HTML API Lifecycle Details

Short description:

You'd attach a callback for necessary changes. Dispose unneeded states, prune removed modules, and invalidate unexpected changes. Lifecycle involves disposing, accepting for replacement, updating, and pruning modules. Accept new modules, handle imported changes, and mutate states using hooks.

So you'd attach a callback so that you do the necessary changes there. There's a disposal if you need to dispose any unneeded states. For example, you have a state there is no longer use before the updates, you do it there. There's a prune if you have a module that's removed. For example, if you remove an import, and you don't need it, you can remove the state completely. And there's also invalidate. This is less a hook, but more like a like an action. But we touch on it later, where if you have the changes suddenly not going as expected, you can call this to restart the loop HTML again. So you can look at the graph on the right side, you can see that when you have a file, you can set up the HTML callbacks here. So you can use the hooks around here with these APIs. And then whenever update the file, you call dispose, and then you call accept to do the replacement, and then we update again and perform the same thing again. And then we delete the file, you dispose, and then finally you call prune. So this is more or less the lifecycle for each modules.

So accept, you can have two signatures, if you're using practice, you can accept the current module. So a new module comes in you accept it and the replacement. You can also accept changes from the imported modules. Maybe this is what you care about, you can do the changes there. And then you mutate any state that you want. And this hook usually just handle a vplugin, so if you install a react-vue-svelte-vplugin, this is basically what they're doing under the hook. It's a bit more isolated because components are more isolated in Vue-Svelte components. So the changes are more idiomatic than what's shown here, but roughly they're using this hook to do the changes that they need to. And this is the most important part of the HTML API to think about for accepting. There's also dispose and prune.

Here's an example. You can use dispose to reset a global state if you're not using it. You can also delete the state if the module is not used at all. So this is some ways that you can also use the hook callbacks and there's also invalid. So in this example here, you try to accept callback, but something strange is not working. So you're gonna bail out, and then invalidate. So this is just one use case, you can use invalidate for.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

Vite: Rethinking Frontend Tooling
JSNation Live 2021JSNation Live 2021
31 min
Vite: Rethinking Frontend Tooling
Top Content
Vite is a next-generation build tool that leverages native ES modules for improved performance. It eliminates the need for bundling and improves hot module replacement. Vite provides an opinionated default configuration while still allowing advanced customization through plugins. It is framework agnostic and can be used for React and other applications. Vite is being adopted by Next.js and Create React App, and integration with Nuxt 3 offers significant speed improvements.
Welcome to Nuxt 3
Vue.js London Live 2021Vue.js London Live 2021
29 min
Welcome to Nuxt 3
Top Content
Nux3 has made significant improvements in performance, output optimization, and serverless support. Nuxt Bridge brings the Nitro engine for enhanced performance and easier transition between Nuxt 2 and Nuxt Read. Nuxt 3 supports Webpack 5, Bytes, and Vue 3. NextLab has developed brand new websites using Docus technology. Nuxt.js is recommended for building apps faster and simpler, and Nuxt 2 should be used before migrating to Nuxt 3 for stability. DOCUS is a new project that combines Nuxt with additional features like content modules and an admin panel.
Vite and the Future of JavaScript Tooling
JSNation 2025JSNation 2025
23 min
Vite and the Future of JavaScript Tooling
Top Content
Evan Yeo discusses Vite's growth, challenges with dependencies like ES Build and Rollup, and the creation of the bundler Rolldown. The JavaScript ecosystem faces fragmentation, but the company aims for a unified JavaScript stack. Rust is chosen for lower-level development, while JavaScript and TypeScript for high-level APIs. Roldown offers advanced bundling features, outperforming existing tools. Integration with Vite leads to tailored optimizations and significant speed improvements. Vite+ development focuses on creating a comprehensive toolkit. VitePlus integrates TS-down for library bundling, ViteTest for testing, and OX-Lint for linting. Future plans include monorepo awareness, build orchestration, and framework-like features.
10 Years of Independent OSS: A Retrospective
JSNation 2024JSNation 2024
33 min
10 Years of Independent OSS: A Retrospective
This talk is a ten-year retrospective into the growth of the Vue.js framework as an open-source project. It highlights the challenges faced by open-source developers, the importance of finding balance and managing scope, and the collaborative nature of the Vue community. The talk also discusses the development of Vite as a build tool and the vision for a unified JavaScript toolchain. It emphasizes the need for community alignment, contributions, and testing, while acknowledging the challenges of bad actors in the open-source community.
What's New in Vite 6
JSNation US 2024JSNation US 2024
29 min
What's New in Vite 6
Watch video: What's New in Vite 6
Vite, a popular build tool for front-end development, has seen significant growth since its public release in 2021. It has been downloaded 12 million times and has over 1,000 contributors. Vite offers benefits such as a powerful dev server, hot module reloading, and a plugin API based on Rollup. However, there are limitations with server-side rendering that require extra code and handling of SSR flags. Vite's environment API allows for cleaner communication between the browser and server, and plugins can configure and customize specific environments. The future plans for Vite include stabilizing the environment API and integrating Rolldown Vite for faster builds. Vite is compatible with various front-end frameworks and is used by projects like Vaku and Vinci. The environment API has potential for per-local builds and building on older versions of Node, but may not be recommended for internationalization purposes.
The Inner Workings of Vite Build
DevOps.js Conf 2022DevOps.js Conf 2022
31 min
The Inner Workings of Vite Build
Welcome to vidBuild, a tool that optimizes your application for production by offering fast hodgemodule replacement and support for various technologies. The build process in vidBuild involves optimizing and minifying assets, bundling JS and CSS, and generating chunks for dynamic imports. The pipeline in vidBuild includes plugins for alias, resolution, CSS modules, and asset handling. Vid is a complete build tool with a flexible plugin system and support from a vibrant community. Vite's plugin API is compatible with Rollup, and Vite aims for simplicity while pushing complexity to the plugin system.