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?
answer
- old instance is never unloaded
- top-level code runs again
- side effects outlive the module
- cleanup runs before the new version
- per-module data handed over
basics
~20 sA 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.
solid answer
~40 sA swap executes the module's new version in the running page but never unloads the old instance, so whatever its top-level code started (an interval, listeners, a socket) keeps running, and the new version starts another copy. Module-scope variables such as the data buffer are fresh bindings in the new instance, so their contents are gone. HMR runtimes give the module two tools: a **cleanup callback** that runs before the new version executes, where you clear the interval and remove listeners, and a **hand-over data object** kept per module, which the cleanup fills and the next instance reads back. Separately, other modules that imported the old exports still hold the old values, so when others hold a long-lived object, accept in the module that owns it and swap the implementation into it.
go deeper
Recall that a hot swap runs the module's code again without unloading the old copy, so timers and listeners can pile up.
Explain the cleanup callback that runs before the new version, the hand-over data object, and which modules re-run during a swap.
Diagnose duplicated side effects after edits, write disposable modules, and choose where to accept so that holders of long-lived objects keep working.
Decide which patterns the codebase standardises on, such as owners of long-lived objects accepting their dependencies, so hot swaps stay trustworthy as the app grows.
## What a self-accepting swap actually does When a module **accepts its own updates**, a hot module replacement (HMR) swap re-executes the module's new version inside the running page. Nothing unloads the old instance. Its top-level code ran once and set things in motion, and those things still exist after the swap: - a timer started with `setInterval` or a pending `setTimeout`; - event listeners added to `window` or `document`; - an open socket or event stream; - observers, DOM nodes appended outside a framework's tree, and entries added to a global registry. The new version's top-level code runs too, and starts a **second** copy of each. A live chart module that polls its API every few seconds therefore polls twice as often after one edit and three times as often after two. The page looks slow or flickery for reasons that have nothing to do with the code that was changed. ## Module-scope state starts again The same re-execution explains the lost data. A `let points = []` at the top of the module is a fresh binding in the new instance, so the buffer of data points the chart had collected is gone. A swap keeps the **page** alive, not the module's own variables. ## The two pieces of machinery that fix it HMR runtimes give a module two tools for exactly this situation: 1. A **cleanup callback** that runs **before** the new version executes. It is where the old instance releases what it acquired: clear the interval, remove the listeners, close the socket. 2. A **hand-over data object** that the runtime keeps per module and passes from one instance to the next. The cleanup callback writes the values worth keeping into it, the new instance reads them back when it starts, and on the very first run it finds nothing there. With both in place, an edit behaves as the developer expects: one timer, a buffer that carries on, and a chart that redraws with the new code. The rule of thumb is that anything outliving the module's own evaluation needs a matching line in the cleanup callback. ## Which modules re-run, and which do not | Module | Re-runs on this swap? | Why | |---|---|---| | the edited module | yes | it is what changed | | modules between it and the boundary | yes | they imported the old version | | a self-accepting boundary | yes | accepting itself means running again | | a boundary that accepts a dependency | no | it keeps running; only its callback is invoked | | unrelated, unchanged modules | no | reused as they are | ## Stale references held elsewhere Re-running a module does not reach into the modules that imported it. If the dashboard imported `points` or a `chart` object before the edit, it still holds the **old instance's** value. An ES module binding is live only within one module instance, and the swap created a new instance. That decides where to accept: - Self-accept a module that only **causes effects**, such as a render loop or a route table it registers itself. - When other code holds what a module exports (a store, a router, a registry), accept the dependency in the module that **owns the long-lived object**, keep that object's identity, and swap the new implementation into it. State and subscribers then survive the edit. ## Pitfalls - The hand-over data is passed as the **same object**, not serialised. Storing a whole component tree or a closure keeps the old instance's memory alive; copy out plain values. - Cleanup that runs **after** the new version starts leaves a moment with two timers and two listeners; it has to come first, which is when HMR runtimes call it. - HMR code belongs inside the development-only guard, so that a production build drops it along with the cleanup logic. - Debugging "the chart got slower" by profiling the new code wastes time: count the timers and listeners first, because duplicates from earlier instances are the usual cause.
- Which side effects need a cleanup step before a hot swap?Anything that outlives the module's own evaluation: intervals and timeouts, event listeners on `window` or `document`, open sockets and event streams, observers, DOM nodes appended outside a framework's tree, and entries added to a global registry. Each needs a matching line in the cleanup callback, or every edit adds another copy.
- When is accepting a dependency better than having the module accept itself?When other modules hold what this module exports. Self-accepting re-runs the module but leaves those holders with old values. Accepting in the module that owns the long-lived object (a store, a router, a registry) lets that object keep its identity while the new implementation is swapped into it, so its state and subscribers survive.
- Why copy plain values into the hand-over data instead of storing whole objects?The data is handed over as the same object, not serialised, so whatever you store keeps everything it references alive, including old DOM trees, closures and the previous instance's caches. Copying out only the values you need avoids carrying a growing chain of old instances across a day of edits.
- A hot swap unloads the old module, and its timers stop with it
- Module-scope variables keep their values across a self-accepting swap
- Importers automatically see a self-accepting module's new exports
- The cleanup callback runs after the new version has started