From 3a087d8fbcd946336f8a0423203967e53e5678fe Mon Sep 17 00:00:00 2001
From: Chris Swithinbank
Date: Wed, 16 Apr 2025 11:27:13 +0200
Subject: Don’t set defaults for `attrs` and `content` in head entries (#3122)
---
.changeset/chilly-dolphins-clap.md | 24 +++++++++++
.../basics/starlight-page-route-data.test.ts | 1 -
packages/starlight/__tests__/head/head.test.ts | 50 ++++++++--------------
packages/starlight/__tests__/i18n/head.test.ts | 1 -
packages/starlight/schemas/head.ts | 4 +-
packages/starlight/utils/head.ts | 16 +++++--
6 files changed, 57 insertions(+), 39 deletions(-)
create mode 100644 .changeset/chilly-dolphins-clap.md
diff --git a/.changeset/chilly-dolphins-clap.md b/.changeset/chilly-dolphins-clap.md
new file mode 100644
index 00000000..35418ec5
--- /dev/null
+++ b/.changeset/chilly-dolphins-clap.md
@@ -0,0 +1,24 @@
+---
+'@astrojs/starlight': minor
+---
+
+Removes default `attrs` and `content` values from head entries parsed using Starlight’s schema.
+
+Previously when adding `head` metadata via frontmatter or user config, Starlight would automatically add values for `attrs` and `content` if not provided. Now, these properties are left `undefined`.
+
+This makes it simpler to add tags in route middleware for example as you no longer need to provide empty values for `attrs` and `content`:
+
+```diff
+head.push({
+ tag: 'style',
+ content: 'div { color: red }'
+- attrs: {},
+});
+head.push({
+ tag: 'link',
+- content: ''
+ attrs: { rel: 'me', href: 'https://example.com' },
+});
+```
+
+This is mostly an internal API but if you are overriding Starlight’s `Head` component or processing head entries in some way, you may wish to double check your handling of `Astro.locals.starlightRoute.head` is compatible with `attrs` and `content` potentially being `undefined`.
diff --git a/packages/starlight/__tests__/basics/starlight-page-route-data.test.ts b/packages/starlight/__tests__/basics/starlight-page-route-data.test.ts
index 6ac530b3..950eeab4 100644
--- a/packages/starlight/__tests__/basics/starlight-page-route-data.test.ts
+++ b/packages/starlight/__tests__/basics/starlight-page-route-data.test.ts
@@ -99,7 +99,6 @@ test('adds custom frontmatter data to route shape', async () => {
"content": "test",
"name": "og:test",
},
- "content": "",
"tag": "meta",
},
]
diff --git a/packages/starlight/__tests__/head/head.test.ts b/packages/starlight/__tests__/head/head.test.ts
index 8ed7e1a7..4c51a083 100644
--- a/packages/starlight/__tests__/head/head.test.ts
+++ b/packages/starlight/__tests__/head/head.test.ts
@@ -28,7 +28,6 @@ test('includes custom tags defined in the Starlight configuration', () => {
defer: true,
src: 'https://example.com/analytics',
},
- content: '',
tag: 'script',
});
});
@@ -41,7 +40,6 @@ test('includes description based on Starlight `description` configuration', () =
name: 'description',
content: 'Docs with a custom head',
},
- content: '',
});
});
@@ -54,7 +52,6 @@ test('includes description based on page `description` frontmatter field if prov
content:
'Learn how Starlight can help you build greener documentation sites and reduce your carbon footprint.',
},
- content: '',
});
});
@@ -66,14 +63,13 @@ test('includes `twitter:site` based on Starlight `social` configuration', () =>
name: 'twitter:site',
content: '@astrodotbuild',
},
- content: '',
});
});
test('merges two