Skip to content

Add head config validation for meta tags with content#3380

Merged
delucis merged 5 commits intowithastro:mainfrom
HiDeoo:hd-feat-meta-content-validation
Sep 19, 2025
Merged

Add head config validation for meta tags with content#3380
delucis merged 5 commits intowithastro:mainfrom
HiDeoo:hd-feat-meta-content-validation

Conversation

@HiDeoo
Copy link
Copy Markdown
Member

@HiDeoo HiDeoo commented Aug 13, 2025

Description

Just had to debug an issue where a user had a head configuration including a meta tag with some content which is invalid HTML (it's a void element) and found out we already have a feature request to better handle this case so I decided to implement it.

This PR adds a validation steps for meta tags in the head config to ensure that content is not provided and displays a friendly error message if it is.

The error message is different if some attrs are provided or not, to help users fix their config more easily:

with attrs without attrs
SCR-20250813-ohai SCR-20250813-ogum

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Aug 13, 2025

🦋 Changeset detected

Latest commit: 1aa8241

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

This PR includes changesets to release 1 package
Name Type
@astrojs/starlight Minor

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

@netlify
Copy link
Copy Markdown

netlify bot commented Aug 13, 2025

Deploy Preview for astro-starlight ready!

Name Link
🔨 Latest commit 1aa8241
🔍 Latest deploy log https://app.netlify.com/projects/astro-starlight/deploys/68a065f1647c120008c641c7
😎 Deploy Preview https://deploy-preview-3380--astro-starlight.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 100 (no change from production)
Accessibility: 100 (no change from production)
Best Practices: 92 (no change from production)
SEO: 100 (no change from production)
PWA: -
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify project configuration.

@github-actions github-actions bot added the 🌟 core Changes to Starlight’s main package label Aug 13, 2025
Copy link
Copy Markdown
Member

@delucis delucis left a comment

Choose a reason for hiding this comment

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

I’d forgotten about my own feature request 😄 But that’s why notes are a good idea.

Thanks for picking it up — looks solid!

(I also double checked if there‘s a better way to display the suggested config in the error messages, but Astro’s error renderer doesn’t have code block support, so we can’t easily style it any better than this 👍)

`The \`head\` configuration includes a \`meta\` tag with \`content\` which is invalid HTML.\n` +
`You should instead use a \`content\` attribute ` +
(Object.keys(rest.attrs ?? {}).length === 0
? 'with an additional attribute to identify the kind of metadata it represents '
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Wonder if we should hint at common attributes here?

Suggested change
? 'with an additional attribute to identify the kind of metadata it represents '
? 'with an additional attribute such as `name`, `property`, or `http-equiv` to identify the kind of metadata it represents '

I also wondered about linking MDN’s <meta> docs but not sure it’s useful.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Sounds like a great idea, I wanted to avoid only mentioning name in the sentence but multiple seems helpful 👍

Regarding the link, I initially included it, but it added a lot of content to the message why being not very helpful in contrast to the "code block" imo so I decided to not include it, no strong opinion on that though.

@delucis delucis added the 🌟 minor Change that triggers a minor release label Aug 13, 2025
@delucis delucis added this to the v0.36 milestone Aug 13, 2025
HiDeoo and others added 2 commits August 14, 2025 09:13
Co-authored-by: delucis <357379+delucis@users.noreply.github.com>
Co-authored-by: delucis <357379+delucis@users.noreply.github.com>
@HiDeoo
Copy link
Copy Markdown
Member Author

HiDeoo commented Aug 14, 2025

Thanks for the review 🙌

(I also double checked if there‘s a better way to display the suggested config in the error messages, but Astro’s error renderer doesn’t have code block support, so we can’t easily style it any better than this 👍)

Yeah, added a todo about that to play with at some point and see if I can come up with an RFC to improve that, feels like it could be helpful in some cases (and not the first time I wanted something similar).

I also tried playing with different output format, e.g. config vs frontmatter, and while it's easy to distinguish between the two, it gets tricky with frontmatter format between JSON, YAML, and TOML, so I think the JSON format should be a good enough indicator of what the fix should look like.

@delucis
Copy link
Copy Markdown
Member

delucis commented Aug 14, 2025

I also tried playing with different output format

Oh, good point! I didn’t even put two and two together that this same JSON would display also when you make the error in your frontmatter.

I guess we could make the schema dynamic based on where it’s used maybe?

So, head({ format: 'js' }) and head({ format: 'md' }) or something like that return a schema that throws the relevant error? Then we could at least show YAML for frontmatter errors which I guess is the most common syntax (and what we show in our docs).

@HiDeoo
Copy link
Copy Markdown
Member Author

HiDeoo commented Aug 14, 2025

So, head({ format: 'js' }) and head({ format: 'md' }) or something like that return a schema that throws the relevant error? Then we could at least show YAML for frontmatter errors which I guess is the most common syntax (and what we show in our docs).

We definitely could, that's even how I initially coded it, then thought about the different Markdown possible format and remove it but I can definitely quickly add it back, and decide how we feel about it 👍

@delucis
Copy link
Copy Markdown
Member

delucis commented Aug 14, 2025

thought about the different Markdown possible format

I think Astro only supports YAML and TOML? And TOML is pretty recent and I’d guess fairly rare? So YAML seems a safe bet to me.

@HiDeoo
Copy link
Copy Markdown
Member Author

HiDeoo commented Aug 14, 2025

And TOML is pretty recent and I’d guess fairly rare?

And I assume that one is even more rare 😅

image

@HiDeoo
Copy link
Copy Markdown
Member Author

HiDeoo commented Aug 14, 2025

Updated the PR with the discussed changes, here is the new output:

with attrs without attrs
SCR-20250814-quhk SCR-20250814-qumy
SCR-20250814-qvbl SCR-20250814-qvhx

While the JSON output seems fine, I wonder if for the YAML one, we should not include more context, e.g.:

head:
  - tag: meta
    attrs:
      name: test:id
      content: '1234'

Instead of just:

tag: meta
attrs:
  name: test:id
  content: '1234'

I also wonder if the start of the sentence should be different between the two cases, e.g.:

The \`head\` configuration includes…
The \`head\` frontmatter field includes…

Any thoughts on this?

@delucis
Copy link
Copy Markdown
Member

delucis commented Aug 15, 2025

While the JSON output seems fine, I wonder if for the YAML one, we should not include more context

I actually like the current one from the screenshots. If you include the head: line, it might look like it’s the first item always? Although maybe the - could be helpful?

- tag: meta
  attrs:
    name: test:id
    content: '1234'

Definitely would be clearer with some code block styling to distinguish it.

I also wonder if the start of the sentence should be different between the two cases

I’m fairly neutral here as “configuration” could be interpreted OK in the frontmatter context where the lines above show more clearly that it’s about the specific entry. But yeah “frontmatter field” does slightly more hint where to look.

Co-authored-by: delucis <357379+delucis@users.noreply.github.com>
@HiDeoo
Copy link
Copy Markdown
Member Author

HiDeoo commented Aug 16, 2025

Although maybe the - could be helpful?

- tag: meta
  attrs:
    name: test:id
    content: '1234'

Definitely would be clearer with some code block styling to distinguish it.

Awesome idea, just pushed that change which I think is enough now.

SCR-20250816-lsbg

Copy link
Copy Markdown
Member

@delucis delucis left a comment

Choose a reason for hiding this comment

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

Forgot to approve this one but I think it’s looking fantastic 🤩

@delucis delucis added the ✅ approved Pull requests that have been approved and are ready to merge when next cutting a release label Aug 28, 2025
@delucis delucis merged commit 3364af3 into withastro:main Sep 19, 2025
15 checks passed
@astrobot-houston astrobot-houston mentioned this pull request Sep 19, 2025
HiDeoo added a commit to HiDeoo/starlight that referenced this pull request Oct 2, 2025
* main: (26 commits)
  i18n(ja): resources/themes (withastro#3442)
  add openstatus showcase (withastro#3436)
  [ci] release (withastro#3434)
  Add `head` config validation for `meta` tags with `content` (withastro#3380)
  Fix Astro Island margins in Markdown (withastro#3340)
  Ensure tab links span the full tab height (withastro#3427)
  Move `<summary>` to top of `<details>` for validity (withastro#3423)
  Run all tests on CI workflow changes (withastro#3433)
  [ci] format
  i18n(fr): small rewording and fixes in top-level pages (withastro#3432)
  i18n(fr): small rewording and fixes in `guides` (withastro#3431)
  i18n(fr): fix typo and links in `reference` files (withastro#3429)
  i18n(fr): small rewording in `components` (withastro#3430)
  [ci] release (withastro#3384)
  i18n(de): Update `page.lastUpdated` translation to be more in‐line with the English translation (withastro#3416)
  Remove invalid value attribute from `<select>` (withastro#3421)
  Convert invalid `<div>` child of `<summary>` to `<span>` (withastro#3422)
  [i18nIgnore] Fix typo in Spanish docs documentation (withastro#3408)
  chore(deps): update actions/labeler action to v6.0.1 (withastro#3407)
  [i18nIgnore] Fix duplicate i18n collection definition (withastro#3409)
  ...
HiDeoo added a commit to HiDeoo/starlight that referenced this pull request Oct 7, 2025
* main: (72 commits)
  Showcase: Add Saucer (withastro#3454)
  i18n(ja): resources/themes (withastro#3442)
  add openstatus showcase (withastro#3436)
  [ci] release (withastro#3434)
  Add `head` config validation for `meta` tags with `content` (withastro#3380)
  Fix Astro Island margins in Markdown (withastro#3340)
  Ensure tab links span the full tab height (withastro#3427)
  Move `<summary>` to top of `<details>` for validity (withastro#3423)
  Run all tests on CI workflow changes (withastro#3433)
  [ci] format
  i18n(fr): small rewording and fixes in top-level pages (withastro#3432)
  i18n(fr): small rewording and fixes in `guides` (withastro#3431)
  i18n(fr): fix typo and links in `reference` files (withastro#3429)
  i18n(fr): small rewording in `components` (withastro#3430)
  [ci] release (withastro#3384)
  i18n(de): Update `page.lastUpdated` translation to be more in‐line with the English translation (withastro#3416)
  Remove invalid value attribute from `<select>` (withastro#3421)
  Convert invalid `<div>` child of `<summary>` to `<span>` (withastro#3422)
  [i18nIgnore] Fix typo in Spanish docs documentation (withastro#3408)
  chore(deps): update actions/labeler action to v6.0.1 (withastro#3407)
  ...
HiDeoo added a commit to HiDeoo/starlight that referenced this pull request Oct 14, 2025
* main: (87 commits)
  i18n(ja): sync outdated aside page (withastro#3465)
  i18n(ja): sync outdated icon reference (withastro#3467)
  chore(deps): update pnpm/action-setup action to v4.2.0 (withastro#3460)
  Showcase: Add Saucer (withastro#3454)
  i18n(ja): resources/themes (withastro#3442)
  add openstatus showcase (withastro#3436)
  [ci] release (withastro#3434)
  Add `head` config validation for `meta` tags with `content` (withastro#3380)
  Fix Astro Island margins in Markdown (withastro#3340)
  Ensure tab links span the full tab height (withastro#3427)
  Move `<summary>` to top of `<details>` for validity (withastro#3423)
  Run all tests on CI workflow changes (withastro#3433)
  [ci] format
  i18n(fr): small rewording and fixes in top-level pages (withastro#3432)
  i18n(fr): small rewording and fixes in `guides` (withastro#3431)
  i18n(fr): fix typo and links in `reference` files (withastro#3429)
  i18n(fr): small rewording in `components` (withastro#3430)
  [ci] release (withastro#3384)
  i18n(de): Update `page.lastUpdated` translation to be more in‐line with the English translation (withastro#3416)
  Remove invalid value attribute from `<select>` (withastro#3421)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

✅ approved Pull requests that have been approved and are ready to merge when next cutting a release 🌟 core Changes to Starlight’s main package 🌟 minor Change that triggers a minor release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants