Skip to content

define:vars scripts lose all their other attributes (e.g. data-astro-rerun) #94

Description

@nagameTW

What happened?

While upgrading a theme from Astro 5 to 7, scripts combining is:inline data-astro-rerun define:vars={{ … }} silently lost data-astro-rerun in the built HTML. The scripts then no longer re-execute after ClientRouter navigations, so features relying on that pattern (a giscus embed, a page toggle) die after the first SPA navigation. No build error or warning — we only caught it by diffing v5 vs v7 dist/ output.

Minimal reproduction (compiler-level, no project needed)

const c = require("@astrojs/compiler-rs");
const src = `---
const g = { a: 1 };
---
<script is:inline data-astro-rerun define:vars={{ g }}>
  console.log(g);
</script>
<script is:inline data-astro-rerun>
  console.log(1);
</script>
`;
c.transform(src, { filename: "Probe.astro" }).then((o) => console.log(o.code));

Output with @astrojs/compiler-rs 0.3.1 (template part, abbreviated):

<script>(function(){${$$defineScriptVars({ g })} … })();<\/script>   ← attribute dropped
<script data-astro-rerun> … <\/script>                               ← attribute kept

The Astro 5 compiler preserved the remaining attributes on define:vars scripts (<script data-astro-rerun>(function(){…})()).

Root cause

In crates/astro_codegen/src/printer/mod.rs, the define:vars branch prints hardcoded opening tags instead of the element's remaining attributes:

Everything except define:vars / is:inline (and the type="module" it re-emits) is discarded — including data-astro-rerun, id, nonce, and any data-*.

Expected behavior

Attributes other than the compiler directives should be preserved on the emitted <script> tag, matching the v5 compiler's behavior.

Happy to send a PR if that helps.

Versions

astro 7.1.1, @astrojs/compiler-rs 0.3.1, Node 25.3.0, Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions