I’m developing my website with Next.js To upgrade my website’s SEO performance, I’m trying to avoid duplicated meta tags.
My Question
In Next official docs, they say that I can avoid overlapped meta tag by insert key property in meta tag. But this does not work.
<meta name="description" content="~~" key="titleDescription"/>
<meta name="keywords" content="~~" key="titleKeywords"/>
These are default rootDocument meta tags and,
<meta name="description" content={item.product_description} key="titleDescription"></meta>
<meta name="keywords" content={item.brand_name} key="titleKeywords"></meta>
These are dynamically generated meta tags in item pages.
In deployed browser, there are still two description and keywords meta tags in website. I want to avoid duplicated meta tag. Thank you for your help!
4
Answers
UPDATE
Here’s what Next.js docs say regarding the
<Head />
element within the_document.js
file:Source: https://nextjs.org/docs/advanced-features/custom-document
Therefore, Next.js recommends that all the dynamic
<meta />
tags should only be defined on an individual page level.ORIGINAL ANSWEAR (it seemed to work at first, but as it turns out, only at random).
This issue seems to appear when you mix up two ways of closing empty HTML tags in JSX. Notice that your
meta
tags in the_document.js
file are self-closing:<meta />
and the dynamic ones are not:<meta></meta>
.Ensure you are consistent with it (i.e. according to best practices you should convert all empty tags to the self-closing variant) and you should be fine.
I believe you can take a look at this package that helps managing meta tags https://github.com/garmeeh/next-seo in Next.js project. It helped me 😀
For your question, yea try putting the Head in the _app instead of _document. _document is like non mutated one.. it will be always in the website. Cheers 🥂
There are a few things to watch for when using Next.js
Head
components._document
Head
component isnext/document
_document
will not be replaced (can be duplicated) even with akey
identifier_app
Head
component isnext/head
_app
can be in child components_app
can be overridden with thekey
identifierkey
identifier.Examples
_document.{js|jsx|ts|tsx}
Scripts, styles, and other tags you need access to immediately – Next will likely not be fully mounted at this point. You cannot replace tags in the
document/head
component with thekey
attribute.OR self-closing
Head
tagNote: import is from next/document
_app.{js|jsx|ts|tsx}
OR load from a custom component
Note: import is from next/head
some_page.{js|jsx|ts|tsx}
NOT ALLOWED
The head component must be on the page and cannot be a child component. If it’s a child component, it sometimes will not override other tags with the same key property.
some_page.{js|jsx|ts|tsx}
Update: The last example seems to work in some cases with Next 11+, but I have run into a few instances where it duplicates tags. I avoid the last method.
I use Next 12.
Thanks to Sean’s comment here, I was able to progress.
I personally update the tags for SEO purpose according to the consulted page with a component Seo that I crafted.
But to prevent importing it in every pages, I wanted to import it once. It happened that importing this Seo component in pages/_app.js was eventually just duplicating the tags and especially breaking my social media links.
So here is my code.
First the Seo component:
I use it in pages/_document.js:
Then I import this component in pages/_document.js as so:
Then, let me spare you some lines to just look at the return of my functional component App in pages/_app.js. There is no import of Seo component, only a ** Head coming from ‘next/head’ ** is used :
And finally, here is a page I receive data from server side props and that I want to use to change the meta tags related to seo. Here again I don’t show the part of the code that are useless for the matter :
The result I have in the source code is there is no duplicated meta tags and I can share my links on social medias with no image, description or page tilte overlaps