Repository navigation
fix(@angular/build): invalidate Vite module cache for removed dev server output files - #34381
Conversation
There was a problem hiding this comment.
Code Review
This pull request ensures that Vite's cached transform results are released for output files that no longer exist after a rebuild, such as renamed content-hashed chunks. It also introduces a behavior test to verify that old chunks are correctly cleaned up and return a 404 status. The review feedback suggests improving the robustness of the test by updating the regular expression for matching chunk hashes to support lowercase and mixed-case characters.
…ver output files Content-hashed chunks are renamed on every rebuild that changes them. The dev server removed the old names from its own file maps but left their transformed code and source maps in Vite's module graph, so memory grew with each save and the old chunk URLs kept serving stale code. Invalidate the Vite modules for removed files, as is already done for updated files.
f74bfdb to
29ceba0
Compare
|
This pull request has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
PR Checklist
Please check to confirm your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Issue Number: N/A
Lazy and shared chunks are named after their content hash (
chunk-[hash].js), so a rebuild that changes a chunk gives it a new name, along with any hashed chunk that imports it. The incremental build result reports the old names inremoved.In
dev-server/vite/index.ts, removed files are only deleted fromgeneratedFilesandassetFiles. Their entries in Vite's module graph are never invalidated. Updated files are invalidated byinvalidateUpdatedFilesinhmr.ts, but removed names are no longer ingeneratedFilesby then, and Vite's own file watcher is disabled, so nothing cleans them up.As a result:
ng serveis restarted. Memory grows with each save that renames chunks.200and outdated code instead of404.To reproduce: an app with three lazy routes that import one ~210 KB shared module. Edit the shared module 25 times. After each rebuild, request the new chunks as a reloading browser would, then force GC. Each save renames 4 chunks.
@angular/build22.2.3pnpm build --local)The leak per save scales with the size of the renamed chunks and their source maps, so apps with large shared chunks lose more.
What is the new behavior?
For each removed output file, the matching Vite modules are looked up and invalidated, the same way
invalidateUpdatedFilesalready does for updated files. This releases their cached transform results. Requests for old chunk names now return404.The new spec
serve-renamed-chunks_spec.tsloads a lazy chunk, edits the lazy module so the rebuild renames the chunk, and checks that the old URL returns404. It fails without this change (Expected 200 to be 404.) and passes with it.Does this PR introduce a breaking change?
Other information
build-action.tsclears them on that path), so they are not affected by the leak or by this change.invalidateModuleclears the cached transform result but leaves the module node in Vite's maps. That is the remaining ~37 KB per save in the table above. Removing the nodes entirely would need more than invalidation. I kept this change minimal, but I'm happy to extend it if you prefer.