fix(html): preserve extra attributes on script tags during build#21799
Open
o-m12a wants to merge 1 commit intovitejs:mainfrom
Open
fix(html): preserve extra attributes on script tags during build#21799o-m12a wants to merge 1 commit intovitejs:mainfrom
o-m12a wants to merge 1 commit intovitejs:mainfrom
Conversation
Script tags in index.html had all non-standard attributes (like fetchpriority, data-*, integrity) stripped during build because Vite reconstructs the tags with only a hardcoded set of attributes. This collects any attributes not managed by Vite (src, type, async, crossorigin, vite-ignore) and forwards them to the output script tag. Closes vitejs#18061 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.
What this PR solves
Fixes #18061 (also related: #17322, #19469)
When a
<script type="module">tag inindex.htmlhas attributes likefetchpriority,data-*, orintegrity, they are stripped from the build output. This happens because Vite reconstructs script tags with only a hardcoded set of attributes (async,type,crossorigin,src).Implementation
Instead of adding
fetchpriorityas another hardcoded attribute, this preserves all attributes that Vite does not manage internally.extraAttrscollection ingetScriptInfo()— captures any attribute not in the set{src, type, async, crossorigin, vite-ignore}scriptExtraAttrsMap(same pattern as existingisAsyncScriptMap)toScriptTag()via spread into theattrsobjectThis approach also fixes the same class of issue for other attributes (e.g.
integrity,data-*), preventing future reports.Tests
Added a build-only test (
scriptFetchPriority) that verifiesfetchpriority="high"is preserved in the output<script>tag.