Skip to content

Conversation

HiDeoo
Copy link
Member

@HiDeoo HiDeoo commented Aug 22, 2025

Changes

This PR fixes an issue with head content parsing when such content is defined in a component that first renders another component before other elements like <title> when using an expression for the title.

When first encoutering a start tag token for a component before any HTML, we switch to the "in body" insertion mode. After that, when we encounter a start tag token for some head related tags, we use the "in head" insertion mode to parse them correctly. Altho, when we later encounter the associated end tag token, we do not switch back to the "in head" insertion mode, which causes invalid markup to be generated.

For example, considering the following Astro component supposed to be rendered in the head:

---
import Analytics from '../components/Analytics.astro';
---
<Analytics />
<title>{title}</title>
<meta name="description" content="a description" />

The generated code before this fix was invalid (notice the meta tag is inside the title tag):

return $$render`${$$renderComponent($$result, "Analytics", Analytics, {})}
<title>${title}
<meta name="description" content="a description"></title>`;

Testing

I added a new print test case for a similar scenario as the one described in the issue which generates a snapshot file with the expected output after the fix.

Docs

This is a bug fix only, so no docs were added or updated.

Copy link

changeset-bot bot commented Aug 22, 2025

🦋 Changeset detected

Latest commit: baba655

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@astrojs/compiler Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Member

@ematipico ematipico left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great fix!

@HiDeoo HiDeoo merged commit 2a27aca into withastro:main Aug 28, 2025
5 checks passed
@HiDeoo HiDeoo deleted the hd-fix-head-content-component-first branch August 28, 2025 08:08
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.

Closing HTML tag getting removed when there's a component above

2 participants