#id Attribute Disappearing from <script is:inline define:vars={{...}}> in Astro Output

3 messages · Page 1 of 1 (latest)

balmy sleet
#

I'm working on an Astro project and noticed something unexpected regarding <script> tags used with is:inline and define:vars.

When I define a script like this in an .astro component:

Code snippet
<script id="myCustomId" define:vars={{ myVar }} is:inline>
console.log(myVar);
</script>

...the id="myCustomId" attribute does not appear in the final rendered HTML when I 'View Page Source' in the browser. The define:vars part works correctly, and the script content is inlined as expected, but the id is stripped.

Is this the intended behavior for inline scripts with define:vars in Astro? If so, could you explain the rationale behind it? Are there specific types of attributes that Astro preserves or removes from inlined scripts during the build process?

proud rampartBOT
#

Hi @balmy sleet. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

I'm sorry, but the knowledge sources do not explicitly mention whether Astro intentionally strips the id attribute (or other attributes) from <script is:inline define:vars={{...}}> tags in the final output, nor do they provide a rationale for this behavior.

What is documented is that:

  • Using define:vars on a <script> tag implies the is:inline directive, causing the script to be inlined directly into the HTML and not bundled or deduplicated. This is necessary so that the script can rerun with each set of values when the component is used multiple times. However, there is no mention of attribute preservation or removal in this context Script & Style Directives – define:vars.
  • The is:inline directive tells Astro to leave the <script> or <style> tag as-is in the final output HTML, but it also notes that the directive is implied whenever any attribute other than src is used on a <script> or <style> tag. There is no explicit statement about which attributes are preserved or stripped Script & Style Directives.

Given the absence of specific information in the documentation, I cannot confirm whether this is intended behavior or a bug, nor can I provide a rationale for attribute handling in this scenario. If this behavior is critical to your project, I recommend raising an issue on the Astro GitHub repository or checking with the Astro community for clarification.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: