-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Astro Container API renderToString function has inconsistent HTML escaping behavior between build and dev modes #16003
Description
Astro Info
Astro v6.0.7
Vite v7.3.1
Node v22.16.0
System Windows (x64)
Package Manager pnpm
Output static
Adapter none
Integrations @astrojs/mdx (v5.0.2)
If this issue only occurs in one browser, which browser is a problem?
No response
Describe the Bug
I recently used the Astro Container API renderToString function in a project to obtain the HTML string of a Content component, modify it, and then render it on the page using set:html.
After upgrading the project to Astro v6, I noticed that the HTML escaping behavior of renderToString for MDX slots is inconsistent between dev and build modes. In build mode, the HTML content inside MDX slots is escaped.
I used console.log(await Astro.slots.render("default")) to inspect the HTML received by the MDX component, and the output in build mode is:
# build mode in Astro 6
<p>This is a primary-level note card, suitable for highlighting key points.</p>
<p>This is an info-level note card, used for additional explanations.</p>
<p>This is a success-level note card, used to indicate successful actions.</p>
<p>This is a warning-level note card, used for important notices.</p>
<p>This is a danger-level note card, used for risk or error alerts.</p>
However, in dev mode and in both dev and build modes of Astro v5, the output is:
# dev mode in Astro 6 / dev & build mode in Astro 5
<p>This is a primary-level note card, suitable for highlighting key points.</p>
<p>This is an info-level note card, used for additional explanations.</p>
<p>This is a success-level note card, used to indicate successful actions.</p>
<p>This is a warning-level note card, used for important notices.</p>
<p>This is a danger-level note card, used for risk or error alerts.</p>
This appears to be a regression introduced in Astro v6, as the behavior was consistent in Astro v5.
I'm not sure whether this behavior is intentional, but this escaping causes MDX component content inside Content to not render correctly. This causes content rendered via set:html to appear as escaped text instead of actual HTML elements.
What's the expected result?
The behavior should be consistent between dev and build modes, matching the behavior in Astro v5. The HTML content inside MDX slots should not be escaped.
Link to Minimal Reproducible Example
https://stackblitz.com/~/github.com/zkz098/astro-issue-test
Participation
- I am willing to submit a pull request for this issue.