react-helmet icon indicating copy to clipboard operation
react-helmet copied to clipboard

Existing meta tags don't get overriden

Open mhluska opened this issue 7 years ago • 23 comments

I was curious for the rationale here. Ideally I would like to have default meta tags that exist before React is loaded for SEO reasons. But this library forces me to remove them otherwise I'll have duplicates. So then I'm looking at adding server-side rendering which is a huge undertaking just to have some static meta tags. Is there any alternative solution?

mhluska avatar Dec 14 '17 19:12 mhluska

Looks like this is only touching tags with Helmet attribute: https://github.com/nfl/react-helmet/blob/master/src/HelmetUtils.js#L416

I think it's the right thing to do as we can't be sure which tags need to be replaced, and touching code out of it's scope should not be done lightly as it can have spill over impact.

xRahul avatar Dec 22 '17 18:12 xRahul

@mhluska I've gotten around this by doing the following inside of my index.html file.

<meta name="theme-color" content="#004ba0" data-react-helmet="true" />

Seems to only work when the tag is self closing and when data-react-helmet="true" is present.

izakfilmalter avatar Jan 27 '18 15:01 izakfilmalter

This is a good workaround. However, if you do not override it in your child pages, then putting the data-react-helmet in your html file will just cause it to get deleted.

electic avatar Jan 21 '19 23:01 electic

in my index.html have code <meta name="description" content="some text ...."> and in one of the components have code <Helmet> <meta name="description" content={smallText} data-react-helmet="true" /> <title>{name}</title> </Helmet>

when I check the code in google chrome panel I see that title rewrites but description no??? where is the problem???

Zimovets avatar Mar 14 '19 14:03 Zimovets

I tried to delete meta description from index.html and nothing happened, It looks like helmet doesn't see this tag in code, but it see title???

Zimovets avatar Mar 14 '19 14:03 Zimovets

I think this should be documented in the main readme. I feel this is a common enough use case and an easy mistake to make.

Will open a PR.

TheKidCoder avatar May 15 '19 00:05 TheKidCoder

I added the following code to my app to manually remove original header elements:

let originalDescriptionElements = document.querySelectorAll('meta[name=description]');
originalDescriptionElements.forEach(e => {
  // @ts-ignore: ignore html.dataset being unknown
  if (!e.dataset) throw new Error("Helmet: html.dataset is not available in this browser.");
  // @ts-ignore: ignore html.dataset being unknown
  else if (!e.dataset.reactHelmet)
    e.parentNode.removeChild(e);
});

bdombro avatar Jul 30 '19 22:07 bdombro

@mhluska I've gotten around this by doing the following inside of my index.html file.

<meta name="theme-color" content="#004ba0" data-react-helmet="true" />

Seems to only work when the tag is self closing and when data-react-helmet="true" is present.

This fixed my issue of not being able to override the meta description tag. Thank you so much!

thoughtworks-tcaceres avatar Oct 29 '19 16:10 thoughtworks-tcaceres

the solutions does not work for me:

      <Helmet>
        <meta name="description" content={t('projectcalls.homepage.description')} data-react-helmet="true" />
        <title>{t('projectcalls.homepage.title', {
          ns: 'seo'
        })}</title>
      </Helmet>

webmobiles avatar Jan 14 '20 08:01 webmobiles

but i've solved upgrading the lib and using this code, on

componentDidMount() {
    const description = document.querySelector('meta[name="description"]');
    if (description) {
      description.remove();
    }

  }

webmobiles avatar Jan 14 '20 10:01 webmobiles

@TheKidCoder did you ever open that PR?

tommedema avatar May 02 '20 23:05 tommedema

@mhluska I've gotten around this by doing the following inside of my index.html file.

<meta name="theme-color" content="#004ba0" data-react-helmet="true" />

Seems to only work when the tag is self closing and when data-react-helmet="true" is present.

This seems to make the default tag disappear. even in the pages that the meta tag is not overriden, the index.html meta tag isn't showing up in the html code. I really can't see the difference between this workaround and deleting the meta tag in your index.html altogether.

mmahdigh avatar Aug 17 '20 14:08 mmahdigh

I'm using react-helmet-async but I had to use data-rh attribute in the index.html like : <meta name="description" content="Your content" data-rh="true" />

benoitkopp avatar Mar 25 '21 13:03 benoitkopp

I think the problem of metadata not being recognized by SEO tools is not affected by the data-rh="true" attribute or similar, I think it is due to the fact that these SEO tools check the tags directly on the index.html file before it is modified by the React system.

Elius94 avatar Feb 28 '22 08:02 Elius94

Has anyone solved this problem, I feel like there is no difference between adding that property and removing it

CongCong-1228 avatar Aug 04 '22 09:08 CongCong-1228

Hello everyone! To solve this I've written this simple tool

https://www.npmjs.com/package/seo-injector

It changes the output HTML

Elius94 avatar Aug 04 '22 11:08 Elius94

Did anyone get a workaround for this?

Deveshb15 avatar Aug 04 '22 15:08 Deveshb15

wow, react helmet just simply doesn't work

0xwallett avatar Dec 08 '22 11:12 0xwallett

the solutions does not work for me:

      <Helmet>
        <meta name="description" content={t('projectcalls.homepage.description')} data-react-helmet="true" />
        <title>{t('projectcalls.homepage.title', {
          ns: 'seo'
        })}</title>
      </Helmet>

You need to add "data-react-helmet" attribute in index.html which you want to override and not in Helmet. Helmet by default will add this attribute.

vardhaman-ck12 avatar Jun 16 '23 13:06 vardhaman-ck12

This solution is 100% working (you DON'T need to add data-react-helmet) const DESCRIPTION = "your des " <Helmet onChangeClientState={(newState) => { const metaDescription = document.querySelector('meta[name="description"]'); if (metaDescription) { metaDescription.setAttribute('content', DESCRIPTION || ''); } }}>

{ TITLE }
    </Helmet>

fadiakkad avatar Jul 16 '23 18:07 fadiakkad

This solution is 100% working (you DON'T need to add data-react-helmet) const DESCRIPTION = "your des " <Helmet onChangeClientState={(newState) => { const metaDescription = document.querySelector('meta[name="description"]'); if (metaDescription) { metaDescription.setAttribute('content', DESCRIPTION || ''); } }}>

{ TITLE }
    </Helmet>

Worked for me. Thanks a lot <3

devDanielCespedes avatar Jan 12 '24 18:01 devDanielCespedes