Skip to content

Commit c6c0c51

Browse files
HiDeoodelucistrueberryless
authored
Dedupe head sitemap link tags (#3457)
Co-authored-by: Chris Swithinbank <swithinbank@gmail.com> Co-authored-by: Felix Schneider <99918022+trueberryless@users.noreply.github.com>
1 parent 2fec483 commit c6c0c51

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

.changeset/mean-icons-sneeze.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
'@astrojs/starlight': patch
3+
---
4+
5+
Deduplicates sitemap link tags in the head.
6+
7+
When [enabling sitemap](https://starlight.astro.build/guides/customization/#enable-sitemap) in Starlight, a `<link rel="sitemap" href="/sitemap-index.xml">` tag is automatically added to the head of each page. Manually specifying sitemap link tags using the Starlight [`head` configuration option](https://starlight.astro.build/reference/configuration/#head) or the [`head` frontmatter field](https://starlight.astro.build/reference/frontmatter/#head) will now override the default sitemap link tag added by Starlight.
8+
9+
This change ensures that users manually adding the `@astrojs/sitemap` integration to the Astro `integrations` array for more fine-grained control over sitemap generation and also using the [`filenameBase` integration option](https://docs.astro.build/en/guides/integrations-guide/sitemap/#filenamebase) can customize the sitemap link tag in the head.

packages/starlight/__tests__/head/head.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,17 @@ test('merges two <link rel="canonical" href="" /> tags', () => {
8484
]);
8585
});
8686

87+
test('merges two <link rel="sitemap" href="" /> tags', () => {
88+
const customLink = {
89+
tag: 'link',
90+
attrs: { rel: 'sitemap', href: '/sitemap-custom.xml' },
91+
} as const;
92+
const head = getTestHead([customLink]);
93+
expect(head.filter((tag) => tag.tag === 'link' && tag.attrs?.rel === 'sitemap')).toEqual([
94+
customLink,
95+
]);
96+
});
97+
8798
test('does not merge same link tags', () => {
8899
const customLink = {
89100
tag: 'link',

packages/starlight/utils/head.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ function createHead(defaults: HeadUserConfig, ...heads: HeadConfig[]) {
120120
}
121121

122122
/**
123-
* Test if a head config object contains a matching `<title>` or `<meta>` or `<link rel="canonical">` tag.
123+
* Test if a head config object contains a matching `<title>`, `<meta>`, `<link rel="canonical">`
124+
* or `<link rel="sitemap">` tag.
124125
*
125126
* For example, will return true if `head` already contains
126127
* `<meta name="description" content="A">` and the passed `tag`
@@ -135,7 +136,9 @@ function hasTag(head: HeadConfig, entry: HeadConfig[number]): boolean {
135136
return hasOneOf(head, entry, ['name', 'property', 'http-equiv']);
136137
case 'link':
137138
return head.some(
138-
({ attrs }) => entry.attrs?.rel === 'canonical' && attrs?.rel === 'canonical'
139+
({ attrs }) =>
140+
(entry.attrs?.rel === 'canonical' && attrs?.rel === 'canonical') ||
141+
(entry.attrs?.rel === 'sitemap' && attrs?.rel === 'sitemap')
139142
);
140143
default:
141144
return false;

0 commit comments

Comments
 (0)