skip to content

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%

answer

  1. walk up through importers
  2. a module that declares it accepts
  3. self-accept or accept a dependency
  4. every path must hit a boundary
  5. reaching the entry means reload

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.

solid answer

~40 s

An **accept boundary** is a module that has declared it can take an update: either it **accepts itself**, so its new version runs in place, or it **accepts a dependency**, so the dependency's new version runs and the boundary's callback re-applies it without the boundary re-running. On save, the dev server starts at the edited module and walks up through its importers. Each path stops at the first boundary it meets; the edited module and the modules between it and the boundary re-run. If **every** path ends at a boundary, the server sends a hot update. If **any** path reaches the entry without one, it falls back to a full reload, because that code would otherwise keep running the old version. Component and stylesheet modules usually get their accept declarations from tooling.

code

javascript · 17 lines
javascript
// main.js: the entry; accepts nothing
import { mountDashboard } from './dashboard.js';
import { formatRange } from './format-range.js';
document.title = `Analytics: ${formatRange(30)}`;
mountDashboard(document.querySelector('#app'));

// dashboard.js: renders the filter panel; accepts nothing
import { FilterPanel } from './filter-panel.js';
export function mountDashboard(el) { /* renders FilterPanel into el */ }

// filter-panel.js: a component module the framework's dev
// transform marks as accepting its own updates (a boundary)
import { formatRange } from './format-range.js';
export function FilterPanel() { /* shows formatRange(days) */ }

// format-range.js: the file being edited
export function formatRange(days) { return `last ${days} days`; }

go deeper

for a junior

Recall that some modules declare they can take updates, and that an edit nobody accepts ends in a full page reload.

for a middle

Explain the walk up through importers, the two kinds of boundary, which modules re-run, and why one dead-end path forces a reload.

for a senior

Trace a surprise reload to the import path that reaches the entry, and move the boundary or the import so edits swap again.

for a principal

Consider how module structure across a large codebase, such as what the entry imports directly, decides whether the whole team's edits swap or reload.

## The question HMR has to answer When you save a file, a dev server with **hot module replacement (HMR)** must decide whether it can apply the change inside the running page or has to reload the page. The answer comes from the **module graph** (which module imports which) and from which modules have declared that they can take an update. Those modules are **accept boundaries**. ## Two kinds of boundary - A **self-accepting** module declares that it can take updates to *itself*. When it changes, its new version runs in place and the update stops there. Component modules (through a framework's development transform) and stylesheet modules are the usual examples. - A module can instead **accept a dependency**: "when `./format-range.js` changes, call me back with the new version". The dependency's new version runs, the accepting module **is not re-executed**, and its callback re-applies whatever it had built from the old version. In both cases the edited module and every module between it and the boundary run again; a self-accepting boundary runs again too, while a dependency-accepting one does not. Unchanged modules elsewhere in the graph do not run again. ## The walk, step by step 1. Start at the edited module. If it accepts itself, it is its own boundary, and the walk stops. 2. Otherwise visit each **importer** of the module. If that importer accepts this dependency, it is the boundary for this path, and the path stops there. 3. If it does not, repeat from that importer, walking further up the graph. 4. If a path reaches a module with **no importers** (the entry) without finding a boundary, that path is a **dead end**. 5. If **every** path ended at a boundary, the server sends an update naming those boundaries. If **any** path dead-ended, it sends a full reload instead. Step 5 is the rule people miss. A boundary on one path does not rescue the others, because the code on the dead-end path would keep running the old version and the page would be left with a mix of old and new code. ## Reading the example In the snippet, `format-range.js` is imported twice. The path through `filter-panel.js` stops at a boundary, because the framework marks that component module as self-accepting. The path through `main.js` reaches the entry, which accepts nothing. One dead end is enough: editing `format-range.js` reloads the page, while editing `filter-panel.js` itself swaps cleanly. | Edited file | Paths up the graph | Result | |---|---|---| | `filter-panel.js` | itself (self-accepting) | hot swap | | `format-range.js` | to `filter-panel.js` (boundary); to `main.js` (entry) | full reload | | `dashboard.js` | to `main.js` (entry) | full reload | | `main.js` | nothing above it | full reload | There are two ways out: move the title code out of the entry into a module that accepts its own updates, or have the entry accept `./format-range.js` and set the title again in its callback. Either way every path now ends at a boundary. ## Edges of the rule - A module the page has **not loaded yet** is not running in the page, so the walk typically does not need to find a boundary through it. - A boundary can **hand an update on** at run time: after looking at the new version, it signals that it cannot apply this particular change, and the walk continues to its importers as if it were not a boundary. Component tooling does this when an edit changes a module's exports in a way it cannot swap. - A boundary inside an **import cycle** can fail to re-import cleanly, because part of the cycle evaluates again out of its original order; at least one dev server then reloads the page to restore a clean order. - Edits outside the module graph, such as the HTML page or the dev server's own configuration, commonly lead to a reload or a server restart whatever the boundaries say. ## Why the design is this way Walking up only as far as the nearest boundary keeps the set of re-executed modules small, so state elsewhere in the page survives. Falling back to a reload whenever any path dead-ends keeps the page from running inconsistent code. The trade is that the shape of the import graph, not only the file you touched, decides whether you keep your state, which is why one stray import from the entry can turn every edit to a shared helper into a reload.

  • In the example, how would you stop edits to format-range.js from reloading the dashboard?
    Make the path through the entry end at a boundary. Either move the title code out of `main.js` into a module that accepts its own updates, or have the entry accept `./format-range.js` and set the title again in the callback. Once every importer path stops at a boundary, the dev server can swap instead of reloading.
  • What can a boundary do when it sees an update it cannot apply safely?
    It can hand the update on: after accepting, it signals that this change is one it cannot handle, and the dev server treats it as if it were not a boundary, continuing the walk to its importers, possibly all the way to a reload. Component tooling uses this when an edit changes a module's exports in a way it cannot swap.
  • Why might a dev server reload even though the walk found a boundary inside an import cycle?
    Modules in a cycle depend on the order in which they first evaluated. Re-running part of the cycle can reach bindings that are not initialised yet, so re-importing the boundary may throw. When that happens, at least one dev server gives up on the swap and reloads the page to restore a clean evaluation order.

An accept boundary is like the first door along a delivery route that agrees to sign for a parcel. Every route from the sender must reach a door that signs; if any route gets back to the depot unsigned, the whole delivery is cancelled and done again from scratch.

  • The module you edited is the one that has to accept the update
  • If any one importer accepts the change, the swap succeeds
  • A module that accepts a dependency re-runs itself whenever that dependency changes
  • HMR needs hand-written accept calls in every module to work
  • A full reload always means the edited file had a syntax error