summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormktbsh2024-07-28 02:18:20 +0900
committerGitHub2024-07-27 19:18:20 +0200
commit0b381d53f23c31492cf415057d960f9a5eaa2f3d (patch)
treeb72cd51775a5064ee9dba0d5693d36c18c52e295
parent8c85be11d7328076dbe7d1c71060a5d163aa64b3 (diff)
downloadIT.starlight-0b381d53f23c31492cf415057d960f9a5eaa2f3d.tar.gz
IT.starlight-0b381d53f23c31492cf415057d960f9a5eaa2f3d.tar.bz2
IT.starlight-0b381d53f23c31492cf415057d960f9a5eaa2f3d.zip
Merge <link rel="canonical" /> tags, quick fixes (#2153) (#2154)
Co-authored-by: Chris Swithinbank <swithinbank@gmail.com>
-rw-r--r--.changeset/long-baboons-jam.md5
-rw-r--r--packages/starlight/__tests__/basics/head.test.ts21
-rw-r--r--packages/starlight/utils/head.ts4
3 files changed, 29 insertions, 1 deletions
diff --git a/.changeset/long-baboons-jam.md b/.changeset/long-baboons-jam.md
new file mode 100644
index 00000000..48c560db
--- /dev/null
+++ b/.changeset/long-baboons-jam.md
@@ -0,0 +1,5 @@
+---
+"@astrojs/starlight": patch
+---
+
+Updates `<head>` logic to deduplicate `<link rel="canonical">` tags. This means that custom canonicals set via frontmatter now override the default canonical generated by Starlight.
diff --git a/packages/starlight/__tests__/basics/head.test.ts b/packages/starlight/__tests__/basics/head.test.ts
index db5a454b..999ccce2 100644
--- a/packages/starlight/__tests__/basics/head.test.ts
+++ b/packages/starlight/__tests__/basics/head.test.ts
@@ -11,6 +11,27 @@ describe('createHead', () => {
).toEqual([{ tag: 'title', content: 'Override', attrs: {} }]);
});
+ test('merges two <link rel="canonical" href="" /> tags', () => {
+ expect(
+ createHead(
+ [{ tag: 'link', attrs: { rel: 'canonical', href: "https://example.com" }, }],
+ [{ tag: 'link', attrs: { rel: 'canonical', href: "https://astro.build" }, content: '' }],
+ )
+ ).toEqual([{ tag: 'link', attrs: { rel: 'canonical', href: "https://astro.build" }, content: '' }]);
+ });
+
+ test('does not merge same link tags', () => {
+ expect(
+ createHead(
+ [{ tag: 'link', attrs: { rel: 'stylesheet', href: "primary.css" }, content: '' }],
+ [{ tag: 'link', attrs: { rel: 'stylesheet', href: "secondary.css" }, content: '' }],
+ )
+ ).toEqual([
+ { tag: 'link', attrs: { rel: 'stylesheet', href: "primary.css" }, content: '' },
+ { tag: 'link', attrs: { rel: 'stylesheet', href: "secondary.css" }, content: '' }
+ ]);
+ });
+
for (const prop of ['name', 'property', 'http-equiv']) {
test(`merges two <meta> tags with same ${prop} value`, () => {
expect(
diff --git a/packages/starlight/utils/head.ts b/packages/starlight/utils/head.ts
index 25a20917..c84537c6 100644
--- a/packages/starlight/utils/head.ts
+++ b/packages/starlight/utils/head.ts
@@ -12,7 +12,7 @@ export function createHead(defaults: HeadUserConfig, ...heads: HeadConfig[]) {
}
/**
- * Test if a head config object contains a matching `<title>` or `<meta>` tag.
+ * Test if a head config object contains a matching `<title>` or `<meta>` or `<link rel="canonical">` tag.
*
* For example, will return true if `head` already contains
* `<meta name="description" content="A">` and the passed `tag`
@@ -25,6 +25,8 @@ function hasTag(head: HeadConfig, entry: HeadConfig[number]): boolean {
return head.some(({ tag }) => tag === 'title');
case 'meta':
return hasOneOf(head, entry, ['name', 'property', 'http-equiv']);
+ case 'link':
+ return head.some(({ attrs }) => attrs.rel === 'canonical')
default:
return false;
}