Skip to content

fix(html): preserve extra attributes on script tags during build#21799

Open
o-m12a wants to merge 1 commit intovitejs:mainfrom
o-m12a:fix/preserve-script-attrs
Open

fix(html): preserve extra attributes on script tags during build#21799
o-m12a wants to merge 1 commit intovitejs:mainfrom
o-m12a:fix/preserve-script-attrs

Conversation

@o-m12a
Copy link

@o-m12a o-m12a commented Mar 9, 2026

What this PR solves

Fixes #18061 (also related: #17322, #19469)

When a <script type="module"> tag in index.html has attributes like fetchpriority, data-*, or integrity, 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 fetchpriority as another hardcoded attribute, this preserves all attributes that Vite does not manage internally.

  • Added extraAttrs collection in getScriptInfo() — captures any attribute not in the set {src, type, async, crossorigin, vite-ignore}
  • Stored per HTML entry via scriptExtraAttrsMap (same pattern as existing isAsyncScriptMap)
  • Forwarded to toScriptTag() via spread into the attrs object

This 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 verifies fetchpriority="high" is preserved in the output <script> tag.

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fetchpriority attribute in <script> is stripped from index.html after build

1 participant