skip to content

Dev Servers & Hot Reload

A dev server transforms modules on demand, bundled or served as native ES modules, and swaps edited modules through hot module replacement. Interviewers ask when an edit forces a full reload.

on this pageshow

questions

6

In a web dev server, what is hot module replacement, and how does it differ from a live reload of the page?

level: juniorimportance: must knowfreq 26%

answer

  1. page torn down or kept
  2. file watcher pushes a message
  3. new module runs in the running page
  4. accept boundary applies the change
  5. no boundary means reload fallback

basics

~20 s

Hot 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 s

Both 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

for a junior

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.

for a middle

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.

for a senior

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.

for a principal

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
open as a page

In hot module replacement, how does a dev server decide whether an edit is swapped in place or turns into a full page reload?

level: middleimportance: must knowfreq 20%

basics

~20 s

The dev server walks up from the edited module through its importers until each path reaches an accept boundary, a module that declared it can take the update; if any path reaches the entry without one, the page reloads.

open as a page

How does a bundling dev server differ from an unbundled native-ESM dev server, and where does each pay its cost as an app grows?

level: middleimportance: should knowfreq 17%

basics

~20 s

A bundling dev server builds the whole module graph before serving, so start-up grows with the app; an unbundled server transforms each module when the browser requests it, so start-up stays fast but page loads grow with the modules imported.

open as a page

Why does an unbundled dev server pre-bundle third-party dependencies before serving them, and what makes it redo that work?

level: middleimportance: should knowfreq 10%

basics

~20 s

Pre-bundling converts CommonJS or UMD packages into ES modules the browser can import, and collapses packages built from hundreds of internal files into one module; the result is cached and rebuilt when its inputs, such as the lockfile, change.

open as a page

After several hot module replacement swaps, a self-accepting chart module polls its API once more per swap and loses its buffered data points; why, and what fixes it?

level: seniorimportance: should knowfreq 8%

basics

~20 s

A self-accepting module re-runs on every swap: each new instance starts another timer while the old ones keep running, and module-scope variables start empty. A cleanup callback must stop the old timer and hand the buffer to the next instance.

open as a page

In a component framework's hot swap, why does editing a file that exports both a component and a constant reset state that other edits keep?

level: seniorimportance: should knowfreq 12%

basics

~20 s

A state-keeping swap works only when every export is a component the runtime can swap by identity; importers hold a constant directly, so the tooling hands the update on to them, and the tree they render can remount or the page reloads.

open as a page