Version dev modules with a per-module counter that moves only when content changes

Open
#23,943 2 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@MuhammadAhmad67889 is already working on this.

Since Nov 19, 2026.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Active
Tech stack
typescript, vite
Domain
devtools, frontend

Research direction

Start by tracing the Vite 8.3.5 code paths named in the issue: handleHMRUpdate, updateModules, EnvironmentModuleGraph.invalidateModule, import analysis, the client's importUpdatedModule, transformRequest, and loadAndTransform. Map how module versions and invalidation counters flow through those paths, then check the HMR tests covering no-op saves, reverts, accepting boundaries, and invalidation races. Done means the counter design meets the listed behaviors without missed updates or redundant runs.

Written by the indexing model from the issue text.

Description

feat: hmr
Description

During dev, Vite versions an updated module with the clock. Every change event sets lastHMRTimestamp to the current time on the changed module and on its importers, and import analysis injects it as ?t=<ms>. No step compares the new content to the current content. Four costs follow from that:

  • A save that changes nothing still re-runs code. Writing the same bytes (a formatter that changes nothing, an editor's save-all, a branch switch that restores a file) issues new URLs, new transforms and a fresh evaluation of the module and every importer up to its boundary.
  • Importers above an accepting boundary get the new stamp too, though the browser keeps their instances. Their next import is a new URL, so a lazily imported importer evaluates twice (#23942).
  • The race guards depend on the clock. transformRequest and loadAndTransform compare millisecond timestamps, which tie within one millisecond (#19804) and miss an invalidation recorded in the other timestamp field (#23941).
  • Each version is a new id to anything keyed by id. Plugins and frameworks see the timestamped id and often cache by it or embed it in virtual module ids, so a long dev session adds an entry per save, no-op saves included.
Suggested solution

Give each module node a small integer version, used in the URL as ?hmr=<n>, and move it only when the module's served code can actually differ:

  1. Bump on a real change only. On a change event, hash the file's bytes and compare with the hash stored for that file. Equal bytes end the update: no invalidation, no message to the client.
  2. Compare with the current content only, never with older versions. A revert (A, then B, then A again) still bumps, because A differs from the current B. A content-addressed URL would be wrong here: the browser's module map never evicts a URL, so the reverted module would come back as the old, already evaluated instance and miss the update.
  3. Bump only the modules the browser re-runs: the changed module and its importers up to and including the accepting boundary. Importers above the boundary have their transform invalidated (their code embeds the boundary's new URL) but keep their version, so their URL stays the one the browser holds.
  4. Guard races with a second per-module counter, bumped on every invalidation (file change, reloadModule(), invalidateModule()), in place of lastInvalidationTimestamp. A request records the counter when it starts and stores its result only if the counter has not moved. Counters do not tie, and every invalidation path moves the same field.

Measured costs on an M2 Pro, Node 24:

  • Reading and hashing a changed file at watch time takes about 60 µs for 10 kB and 130 µs for 100 kB, nearly all of it the read. sha256 of the buffer alone is 4 µs at 10 kB.
  • The stored hash adds about 125 bytes per watched file.
  • A small-integer version is smaller than the two heap-number timestamps it replaces on each node.

A model of seven saves of one module (A, A, B, A, B, B, C, A), counting evaluations in the browser:

Versioning Evaluations Real changes Missed updates Redundant runs Module map entries
Clock ?t=<ms> (today) 7 5 0 2 8
Content hash in the URL 3 5 3 1 4
Counter bumped on content change 5 5 0 0 6
Alternative
  • Reuse ?v=. Not possible: DEP_VERSION_RE (/[?&](v=[\w.-]+)\b/) marks such URLs as optimized deps, which are served max-age=31536000,immutable.
  • Keep ?t= with a small counter. Not possible as is: timestampRE (/(\?|&)t=\d{13}(?:&|$)/) only strips 13-digit values, so a counter in t= would leak into module ids.
  • Skip unchanged saves only, keep the clock. That removes the no-op re-runs, but leaves the re-stamp above the boundary, the clock-based race guards and the growing id space.
Additional context

Code references (8.3.5): the timestamp is assigned in handleHMRUpdate and updateModules, propagated in EnvironmentModuleGraph.invalidateModule, injected by import analysis and by the client's importUpdatedModule, and compared in transformRequest and loadAndTransform. #6035 describes the importer chain receiving the timestamp.

Validations
Dominant language
TypeScript
Stars
84k
Forks
9.2k
Avg merge
1d 9h
Merged PRs (30d)
103

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from vitejs/vite

All issues in vitejs/vite

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.