Skip to content

Commit 37dda3c

Browse files
authored
Remove keyoverride attribute from additional meta tags (#1080)
1 parent 0bb10e9 commit 37dda3c

File tree

3 files changed

+52
-14
lines changed

3 files changed

+52
-14
lines changed

README.md

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -574,17 +574,16 @@ additionalMetaTags={[{
574574
}]}
575575
```
576576

577-
One thing to note on this is that it currently only supports unique tags.
578-
This means it will only render one tag per unique `name` / `property` / `httpEquiv`. The last one defined will be rendered.
577+
One thing to note on this is that it currently only supports unique tags, unless you use the `keyOverride` prop to provide a unique [key](https://reactjs.org/docs/lists-and-keys.html#keys) to each additional meta tag.
579578

580-
Example:
579+
The default behaviour (without a `keyOverride` prop) is to render one tag per unique `name` / `property` / `httpEquiv`. The last one defined will be rendered.
581580

582-
If you pass:
581+
For example if you pass 2 tags with the same `property`:
583582

584583
```js
585584
additionalMetaTags={[{
586585
property: 'dc:creator',
587-
content: 'John Doe'
586+
content: 'Joe Bloggs'
588587
}, {
589588
property: 'dc:creator',
590589
content: 'Jane Doe'
@@ -594,7 +593,28 @@ additionalMetaTags={[{
594593
it will result in this being rendered:
595594

596595
```html
597-
<meta property="dc:creator" content="Jane Doe" />,
596+
<meta property="dc:creator" content="Jane Doe" />
597+
```
598+
599+
Providing an additional `keyOverride` property like this:
600+
601+
```js
602+
additionalMetaTags={[{
603+
property: 'dc:creator',
604+
content: 'Joe Bloggs',
605+
keyOverride: 'creator1',
606+
}, {
607+
property: 'dc:creator',
608+
content: 'Jane Doe',
609+
keyOverride: 'creator2',
610+
}]}
611+
```
612+
613+
results in the correct HTML being rendered:
614+
615+
```html
616+
<meta property="dc:creator" content="Joe Bloggs" />
617+
<meta property="dc:creator" content="Jane Doe" />
598618
```
599619

600620
#### Additional Link Tags

src/meta/__tests__/buildTags.spec.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,28 @@ it('additional meta tags are set', () => {
872872
expect(Array.from(httpEquivTag).length).toBe(1);
873873
});
874874

875+
it('uses key override to render multiple additional meta tags with the same key', () => {
876+
const overrideProps: BuildTagsParams = {
877+
...SEO,
878+
additionalMetaTags: [
879+
{ property: 'foo', content: 'Foo 1', keyOverride: 'foo1' },
880+
{ property: 'foo', content: 'Foo 2', keyOverride: 'foo2' },
881+
{ name: 'bar', content: 'Bar 1', keyOverride: 'bar1' },
882+
{ name: 'bar', content: 'Bar 2', keyOverride: 'bar2' },
883+
],
884+
};
885+
const tags = buildTags(overrideProps);
886+
const { container } = render(<>{React.Children.toArray(tags)}</>);
887+
888+
const propertyTags = container.querySelectorAll('meta[property="foo"]');
889+
expect(Array.from(propertyTags).length).toBe(2);
890+
expect(propertyTags[0]).not.toHaveAttribute('keyoverride');
891+
892+
const nameTags = container.querySelectorAll('meta[name="bar"]');
893+
expect(Array.from(nameTags).length).toBe(2);
894+
expect(nameTags[0]).not.toHaveAttribute('keyoverride');
895+
});
896+
875897
it('correctly sets noindex default', () => {
876898
const overrideProps: BuildTagsParams = {
877899
...SEO,

src/meta/buildTags.tsx

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -188,13 +188,9 @@ const buildTags = (config: BuildTagsParams) => {
188188
);
189189
}
190190

191-
if(config.themeColor){
191+
if (config.themeColor) {
192192
tagsToRender.push(
193-
<meta
194-
key="theme-color"
195-
name="theme-color"
196-
content={config.themeColor}
197-
/>
193+
<meta key="theme-color" name="theme-color" content={config.themeColor} />,
198194
);
199195
}
200196

@@ -640,11 +636,11 @@ const buildTags = (config: BuildTagsParams) => {
640636
}
641637

642638
if (config.additionalMetaTags && config.additionalMetaTags.length > 0) {
643-
config.additionalMetaTags.forEach(tag => {
639+
config.additionalMetaTags.forEach(({ keyOverride, ...tag }) => {
644640
tagsToRender.push(
645641
<meta
646642
key={`meta:${
647-
tag.keyOverride ?? tag.name ?? tag.property ?? tag.httpEquiv
643+
keyOverride ?? tag.name ?? tag.property ?? tag.httpEquiv
648644
}`}
649645
{...tag}
650646
/>,

0 commit comments

Comments
 (0)