In a web dev server, what is hot module replacement, and how does it differ from a live reload of the page?
answer
- page torn down or kept
- file watcher pushes a message
- new module runs in the running page
- accept boundary applies the change
- no boundary means reload fallback
basics
~20 sHot module replacement swaps only the edited modules into the running page, so in-memory state such as a half-typed form can survive; a live reload requests the whole page again and re-runs every script, losing that state.
solid answer
~40 sBoth start the same way: the dev server watches files and, on save, pushes a message over a long-lived connection (usually a WebSocket) to a small client script in the page. With a **live reload**, that client reloads the page, so every module runs again and everything in memory is gone. With **hot module replacement (HMR)**, the client fetches the new version of the changed module and executes it inside the running page, and a module that has declared it can take the update (an *accept boundary*) re-renders or swaps the new code in. Typed input, open dialogs and scroll position can survive. HMR is not guaranteed, though: when no module accepts the change, the dev server falls back to a full reload, which is why the same project sometimes swaps and sometimes reloads.
go deeper
Recall the two outcomes of a save: the page reloads and loses its state, or the edited module is swapped in while the page keeps running.
Explain the loop: the file watcher, the pushed message, the client fetching the new module, an accepting module applying it, and a reload when nothing accepts.
Show that you read a reload as information, meaning no boundary took the edit, and that you know which code to change so the swap succeeds next time.
Weigh what reliable hot swaps save a large team against the discipline they demand, such as keeping module side effects disposable across the codebase.
## The loop every dev server runs A **dev server** is the process you keep running while you write code. It serves your source to the browser, transforms whatever the browser cannot run as written (TypeScript, JSX, stylesheet imports), and **watches the file system**. When you save a file, much the same loop starts on most toolchains: 1. The watcher notices the changed file, and the server works out which entries in its **module graph** (the map of which file imports which) are affected. 2. The server pushes a message to a small **client script** it injected into the page, over a long-lived connection, usually a WebSocket. 3. The client decides what to do with that message. This is the point where live reload and hot module replacement part ways. ## Live reload: start the page again With **live reload**, the client always does the same thing: it reloads the page. The browser requests the HTML again, every script runs from the top, and everything that lived only in memory is gone: the half-filled filter form, the open dialog, the scroll position, a subscription and the data it had collected. Live reload is simple and always consistent, because every module starts fresh. On a large app, though, every save costs a full page start plus the time it takes you to click back into the state you were testing. ## Hot module replacement: swap the module With **hot module replacement (HMR)**, the client fetches the new version of the changed module and executes it **inside the running page**. The page is never torn down. How the new code gets in depends on how the server serves modules: - An **unbundled** dev server serves native ES modules, and a browser keeps one instance per module URL with no way to unload or re-evaluate it. So the server hands out the edited file under a **new URL** (typically the same path with a timestamp query) and the client imports that, which creates a fresh instance. - A **bundling** dev server keeps its own module registry inside the bundle's runtime code. It ships replacement code for the changed modules, and that runtime re-runs them in place. Running the new code is only half of a swap. Something also has to *use* it: re-render a component, re-register a route table, re-apply a stylesheet. A module that has declared it can do that is an **accept boundary**, and the dev server walks from the edited module up through its importers to find one. Component frameworks' development transforms add these declarations to component modules for you, which is why most application code never mentions HMR at all. ## What a hot swap keeps, and what it does not | | Live reload | Successful hot swap | |---|---|---| | Page and DOM | rebuilt from scratch | kept; only the affected parts re-render | | Component state (form input, toggles) | lost | usually kept, within the framework's limits | | Module-scope variables of re-run modules | reset | reset, unless handed over to the new instance | | Unchanged modules | re-run | not re-run | | Timers and listeners started by a re-run module | gone with the page | duplicated unless cleaned up first | The last two rows are the price of keeping the page alive. The old instance of a module is not unloaded, so anything it started keeps running unless the module cleans up before the swap. ## Stylesheets: the easy case A stylesheet edit is where HMR most visibly works. The module that stands for the stylesheet **accepts its own updates**, so the client replaces the old rules with the new ones (rewriting a `<style>` element's text, or inserting a fresh `<link>` and removing the old one once the new file has loaded) and none of the app's own JavaScript modules re-run. That is why a colour change appears almost instantly. The exception is a stylesheet whose class names are exported to JavaScript: other modules hold that map, so the stylesheet module cannot simply accept itself. ## When HMR falls back to a reload HMR is best-effort. When the walk from the edited module reaches the app's entry without finding a boundary, nothing in the page can apply the change, and the dev server **falls back to a full reload**. Some edits, such as the HTML page itself, are commonly treated as a reload outright. A reload during development is therefore information, not a malfunction: it says that no boundary took the edit. It is normal for one project to swap one file and reload another, and when the difference matters, the fix is to change where the boundary sits, not to toggle HMR off and on.
- How does the browser end up running new code for a module it has already loaded as a native ES module?A browser keeps one instance per module URL and offers no way to unload or re-evaluate it. An unbundled dev server therefore serves the edited module under a new URL, typically the same path with a timestamp query, and the client imports that, creating a fresh instance. The old instance stays in memory, and code that still references it sees old values unless a boundary swaps the new ones in.
- Why does the client script in the page need a connection back to the dev server at all?File watching happens on the server, so only the server knows that an edit happened and which modules it touched. It pushes an update message, naming the changed modules or asking for a reload, over a persistent socket, and the client then fetches the new code. If the connection drops, some clients poll until the server is back and then reload the page.
- Hot module replacement is just a faster page refresh
- Once HMR is switched on, every edit is hot-swapped
- A full reload during development means the dev server is broken
- A hot swap keeps the values the old module held in top-level variables