Skip to content

Commit

Permalink
Merge <link rel="canonical" /> tags, quick fixes (withastro#2153)
Browse files Browse the repository at this point in the history
  • Loading branch information
mktbsh committed Jul 26, 2024
1 parent 8c85be1 commit e3331bc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/starlight/__tests__/basics/head.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 2 additions & 0 deletions packages/starlight/utils/head.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) => typeof attrs.rel === 'string' && ['canonical'].includes(attrs.rel))
default:
return false;
}
Expand Down

0 comments on commit e3331bc

Please sign in to comment.