[Bug]: lazy compilation changes shared chunk imports and reloads pages without source edits
Problem
Activating a lazily compiled feature can reload an already-running page and discard typed input without a source edit. This occurs with async-only split chunks, a single entry, and unchanged initial chunk membership.
An eager infrastructure module owns an unused dynamic import. Compiling a different feature creates a shared async chunk and changes that infrastructure module’s generated import. HMR then treats it as an update to non-accepting application code and reloads the document.
Expected: lazy activation loads the feature while preserving the running page and its state. Genuine unsafe source updates must retain the normal reload fallback.
Actual: Aborted because ./infrastructure.js is not accepted, followed by a document reload and lost draft.
Runnable reproduction
Complete fixture, exact generated diff, and browser results.
Tested on Linux with Node 24.19.0, Chromium, Rsbuild 2.2.8, and the official Rspack/native binding 2.2.6. The fixture is plain JavaScript and does not use React, React Router, or application code.
gh gist clone fcdacf9936c37608c5a2f7313f8417e5 rspack-lazy-repro
cd rspack-lazy-repro
corepack [email protected] install
RAYON_NUM_THREADS=1 corepack [email protected] dev- Open http://127.0.0.1:9874/ in a fresh tab. The document counter starts at
1. - Type
keep this draftinto the input. - Click Activate feature, without editing any file.
- After compilation, the counter becomes
2and the draft is empty.
The fixture has an eager infrastructure.js that imports other-screen.js dynamically, but never calls that import. Only feature.js is compiled lazily. Both destinations import shared.js. The split group is configured with chunks: 'async' and minChunks: 2.
The entire generated change to the infrastructure function is:
- return __webpack_require__.e("other-screen_js").then(loadOtherScreen);
+ return Promise.all([
+ __webpack_require__.e("shared"),
+ __webpack_require__.e("other-screen_js")
+ ]).then(loadOtherScreen);The fixture includes the exact, unabridged emitted diff. The index and runtime chunks retain identical module membership. The added shared-chunk dependency alone causes this rejection:
Aborted because ./infrastructure.js is not accepted
Update propagation: ./infrastructure.js -> ./index.jsControl
Stop the server, restart with splitting disabled, and repeat in a fresh tab:
REPRO_SPLIT=false RAYON_NUM_THREADS=1 corepack [email protected] dev| Configuration | Documents | Draft | Feature |
|---|---|---|---|
| Async-only splitting | 2 | Lost | Interrupted by reload |
| Splitting disabled | 1 | Preserved | Renders successfully |
Disabling splitting is a diagnostic control, not a proposed general fix. Both cases ran against unmodified compiler and server code. Installed dependencies were reused; the fresh-install commands above were not exercised.
Related graph cases and fix requirements
The earlier two-entry fixture also reloads both open documents when lazy activation reshuffles initial shared chunks with chunks: 'all'. Suppressing its reload guard can leave the second page without required CSS. Async-only splitting passed that particular fixture; the reproduction above shows why it is not a general workaround.
A fix needs to update chunk dependencies without invalidating unrelated application state. Simply accepting every update, suppressing reloads, or comparing source hashes would miss unsafe transitions and location-dependent asset URLs.
Source patch and validation
The Rspack 2.2.6 / Rsbuild 2.2.8 source-only patch package contains exact dependencies, reproducible stock/candidate controls, and the current compact native prototype. Materialize the files and follow its README. The relevant patches are rspack-compact-async-map.patch followed by rspack-scope-stable-map.patch.
The prototype moves changing async block-to-chunk lists into an HMR-updated runtime table. An unchanged importing factory keeps its logical block ID and passes its existing HMR-aware chunk loader. The table uses lossless prefix/suffix encoding for logical IDs and a chunk-ID dictionary.
The map is enabled only when the native lazy and HMR plugins are both active, in development without concatenation. Its semantic choice participates in code-generation cache keys. Using the combined native binding identified in VALIDATION.md (SHA-256 beginning 08eafceb), the packaged two-client browser controls preserve drafts through activation, the previously unused import, feature/shared-source edits, and a verified persistent-cache restart. A genuine nonaccepting infrastructure edit still reloads. Packaged production and concatenation controls verify that the map stays disabled in those modes.
A separate matched browser pair uses Rsbuild 2.2.9 / Rspack 2.2.7. Stock 2.2.7 changes the unused import owner's factory and reproduces the infrastructure reload and lost draft. The custom candidate retains identical owner bytes while updating the runtime map; both clients retain drafts through activation, later imports and accepted feature/shared-source edits. A final nonaccepting infrastructure edit intentionally reloads both clients. Broader candidate lazy coverage also passes, but has no matched broad-stock negative baseline.
The custom native candidate has SHA-256 b6475ccf0c3e67cc795d21ca4f745c40e5945529f8894ab5cd14445cbf090df2. Its matrix has 19 controls directly selecting 2.2.7 plus three cross-version predecessor controls involving 2.2.6. Those three do not isolate same-version cache-key migration. Five disabled-policy comparisons exclude runtime.js; exact runtime equality is checked only for node-off, node-options-only and web-no-hmr. These are local results; the linked public source package remains pinned to 2.2.6 / 2.2.8.
The combined native validation also includes separately documented HMR import-ordering and CSS-ownership patches. Do not attribute the combined outcome solely to the map or apply the optional JavaScript ordering hook to a binding that already contains that native change. The package identifies each patch and its independent controls.
Scope: this remains a development prototype. It does not solve initial asset relocation with chunks:all; the package includes a separate Rsbuild lazy-development default proposal and preserves explicit splitting choices and reload guards. Arbitrary context imports and untested output/loading formats remain outside validation; concatenation is deliberately excluded from the map. Mutable future-generation asset URLs and the inherited incremental:false stale-ESM case remain separate limitations. The public 2.2.6 validation uses an optimized CI-profile native build; the separate 2.2.7 candidate uses a host-linker release build. Neither is an upstream-distribution performance benchmark.
Relevant code: async-import code generation and HMR update selection.
Source: web-infra-dev/rspack