fix(html): resolve modulepreload href for plugin-resolved module ids#22879
Closed
codewithsupra wants to merge 1 commit into
Closed
fix(html): resolve modulepreload href for plugin-resolved module ids#22879codewithsupra wants to merge 1 commit into
codewithsupra wants to merge 1 commit into
Conversation
Fixes vitejs#22845 <link rel="modulepreload"> references a module, the same as a <script type="module">, but its href was only ever handled through the generic asset pipeline (processAssetUrl -> urlToBuiltUrl), which resolves ids as files and silently leaves the href untouched when the file doesn't exist on disk. Module scripts, by contrast, are resolved through this.resolve()/this.load() (via the import injected into the generated JS) so plugin-resolved (virtual) ids work correctly there. This meant a modulepreload pointing at a virtual/plugin-resolved module id (e.g. React's prerenderToNodeStream bootstrapModules emitting both a modulepreload and a script tag for the same server-rendered client entry) kept its original, unresolved href in the build output -- a 404 in production -- while the sibling <script> for the exact same module worked fine, since "real" filesystem modules happened to already exist at that same href. Fix: give modulepreload links their own resolution path. During transform, resolve the href via this.resolve() (same as module scripts) and, when it resolves to a non-external module, replace the href with a placeholder and record which built module it should point at. In generateBundle, once the bundle's chunks are known, look up the chunk containing that resolved module (preferring the chunk it's the facade of, then any chunk that includes it) and substitute the placeholder with that chunk's output path. An href that can't be resolved is left untouched with a warning, so runtime-handled urls (e.g. server middleware responses) keep working as before. Also fixed a String.replace() footgun found while writing this: the existing modulepreload rewrite (and the new one) use a function replacer instead of a plain string, since a filename or original url containing '$' sequences (e.g. '$&', '$1') would otherwise be interpreted as a special replacement pattern and corrupt the output. Tests: added playground/html/modulepreloadResolved.html covering both a plugin-resolved (virtual) modulepreload id and an unresolvable one. Verified the new test fails with the exact bug from the issue (the plugin-resolved href reaches the build output unchanged) by reverting only src/node/plugins/html.ts and rebuilding, then passes again with the fix restored. Full html playground suite (build: 55, serve: 54) and the vite package's unit suite (821 tests) all pass.
Member
|
We first need to understand what the expected behavior is. The semantics of |
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.
Fixes #22845
references a module, the same as a <script type="module">, but its href was only ever handled through the generic asset pipeline (processAssetUrl -> urlToBuiltUrl), which resolves ids as files and silently leaves the href untouched when the file doesn't exist on disk. Module scripts, by contrast, are resolved through this.resolve()/this.load() (via the import injected into the generated JS) so plugin-resolved (virtual) ids work correctly there.This meant a modulepreload pointing at a virtual/plugin-resolved module id (e.g. React's prerenderToNodeStream bootstrapModules emitting both a modulepreload and a script tag for the same server-rendered client entry) kept its original, unresolved href in the build output -- a 404 in production -- while the sibling <script> for the exact same module worked fine, since "real" filesystem modules happened to already exist at that same href.
Fix: give modulepreload links their own resolution path. During transform, resolve the href via this.resolve() (same as module scripts) and, when it resolves to a non-external module, replace the href with a placeholder and record which built module it should point at. In generateBundle, once the bundle's chunks are known, look up the chunk containing that resolved module (preferring the chunk it's the facade of, then any chunk that includes it) and substitute the placeholder with that chunk's output path. An href that can't be resolved is left untouched with a warning, so runtime-handled urls (e.g. server middleware responses) keep working as before.
Also fixed a String.replace() footgun found while writing this: the existing modulepreload rewrite (and the new one) use a function replacer instead of a plain string, since a filename or original url containing '$' sequences (e.g. '$&', '$1') would otherwise be interpreted as a special replacement pattern and corrupt the output.
Tests: added playground/html/modulepreloadResolved.html covering both a plugin-resolved (virtual) modulepreload id and an unresolvable one. Verified the new test fails with the exact bug from the issue (the plugin-resolved href reaches the build output unchanged) by reverting only src/node/plugins/html.ts and rebuilding, then passes again with the fix restored. Full html playground suite (build: 55, serve: 54) and the vite package's unit suite (821 tests) all pass.