withastro / starlight

🌟 Build beautiful, accessible, high-performance documentation websites with Astro
https://starlight.astro.build
MIT License
5.01k stars 532 forks source link

[regression] Starlight mdx files no longer properly combine script tags present in nested astro Components (build mode only) #1883

Closed david-hodgetts closed 6 months ago

david-hodgetts commented 6 months ago

What version of starlight are you using?

0.22.3

What version of astro are you using?

4.8.4

What package manager are you using?

npm

What operating system are you using?

Linux

What browser are you using?

Not relevant

Describe the Bug

Given the following Starlight mdx file:

---
// src/content/docs/issue.mdx
title: the issue
---

import Example from '../../examples/Example.astro';

<Example/>

Which uses the following Example Astro component:


---
// src/examples/Example.astro
import Wrapper from "../components/Wrapper.astro";
---

<Wrapper>
  <script>
    console.log("running in example");
    document.getElementById('my-square')!.style.backgroundColor = "blue";
  </script>
  <p>The square below is expected to be blue at runtime.</p>
  <div id="my-square" class="square"></div>
</Wrapper>

<style>
  .square {
    width: 100px;
    height: 100px;
    background-color: red;
  }
</style>

Which itself contains an instance of the following Wrapper Astro component:

---
// src/components/Wrapper.astro
---
<slot />

<script>
  console.log("this script tag is part of the issue");
</script>

We expect both script tags present in the Wrapper and Example component to run. This is, however, not the case when when running the project from a build folder. In that situation, the script tag present in the Examplecomponent 'disappears' and only the script tag present in the Wrapper component runs.

The issue does not occur when running through the dev server.
The issue does not occur when the Example component is inserted in an Astro page.
The issue appears only when using Astro v4.8.0 or higher.

Link to Minimal Reproducible Example

https://github.com/david-hodgetts/starlight-mdx-with-astro-component-script-tag-issue

Participation

david-hodgetts commented 6 months ago

I think this is a duplicate of #11063. So this is not a starlight issue per se. Sorry for posting in the wrong repo.

Masty88 commented 6 months ago

Thank you david i'm running into the same issue!

delucis commented 6 months ago

Thanks for reporting @david-hodgetts! I also think it’s a duplicate of https://github.com/withastro/astro/issues/11063, so will close this one in favour of that one — probably nothing we can do in Starlight itself.

For anyone else finding this, I guess until https://github.com/withastro/astro/issues/11063 is fixed, downgrading to Astro 4.7 might be the best approach.