fix(optimizer): stabilize server dep outputs during discovery#21818
Open
agcty wants to merge 4 commits intovitejs:mainfrom
Open
fix(optimizer): stabilize server dep outputs during discovery#21818agcty wants to merge 4 commits intovitejs:mainfrom
agcty wants to merge 4 commits intovitejs:mainfrom
Conversation
7 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #19323
Summary
When a non-browser dev environment discovers new optimized dependencies during request handling, the dep optimizer can reshuffle shared chunks and rewrite previously optimized server entry outputs.
That causes Vite to treat the rerun as incompatible and trigger the full-reload path, which is acceptable for browsers but unsafe for in-flight SSR/RSC requests.
This change makes server-side dep optimization runs with runtime discovery enabled preserve module boundaries by default. That keeps previously optimized server entry outputs stable when new deps are added, so the optimizer can commit the rerun without forcing a reload.
Scope
This only applies when:
serveroptimizeDeps.noDiscovery === falseClient dep optimization is unchanged, and explicit server no-discovery prebundles are unchanged.
Implementation
output.preserveModules = trueoutput.preserveModulesRoottoenvironment.config.rootpreserveModules/preserveModulesRootoverrides intactWhy this approach
The bug is caused by unstable optimizer output for long-lived server environments, not by missing late runtime error handling.
Stabilizing the optimizer output graph is a smaller and more architectural fix than trying to recover after modules have already executed with stale chunk references.
Tradeoff
This gives up some bundling compactness for server dep optimization in exchange for stable outputs:
deps_ssr/deps_rscThat tradeoff is intentionally limited to server environments with runtime discovery, where correctness matters more than minimizing the number of optimized cache files.
Tests
Added a regression test that builds a synthetic dep graph where:
Validated with:
pnpm exec vitest run packages/vite/src/node/__tests__/optimizer.spec.tspnpm exec vitest run packages/vite/src/node/__tests__/scan.spec.tspnpm exec vitest run packages/vite/src/node/ssr/__tests__/ssrLoadModule.spec.tspnpm exec vitest run packages/vite/src/node/__tests__/environment.spec.tsI also verified the Cloudflare/RSC repro locally against this Vite build. Late
@conform-to/*discovery still occurs, but the rerun now logsoptimized dependencies unchangedinstead ofoptimized dependencies changed. reloading.