0

I have different meta tags in my React page:

import Head from 'next/head'

export function MyComponent(): JSX.Element {
  const title = getTitle()
  const description = getDescription()
  const image = getImage()
  const url = getUrl()

  return (
        <Head>
            <title>{`${title} • MyWebsite.com`}</title>
            <Description description={description} />
            <OpenGraph
                title={title}
                description={description}
                image={image}
                url={url}
            />
            <Twitter
                title={title}
                description={description}
                image={image}
                url={url}
            />
        </Head>
    )
}

All of them seems to work fine (I can see the tags when I inspect the page), but the Twitter one doesn't work at all. <Twitter /> component looks almost the same as the <OpenGraph />:

export function Twitter({
    card,
    title,
    description,
    type,
    image,
    url,
}: TwitterProps): JSX.Element {
    return (
        <>
            <meta
                property="twitter:card"
                content={card ?? 'default'}
            />
            <meta
                property="twitter:title"
                content={title}
            />
            <meta
                property="twitter:description"
                content={description}
            />
            <meta
                property="twitter:type"
                content={type ?? 'default'}
            />
            {image ? (
                <meta
                    property="twitter:image"
                    content={image}
                />
            ) : null}
            <meta
                property="twitter:url"
                content={url}
            />
        </>
    )
}

When I inspect the rendered page, I can see the title, <meta property="description> and all desired og meta tags but none of Twitter. How do I show the Twitter tags properly?

3
  • Does this answer your question? Avoid Duplicate Meta Description and Keywords in Next.js Commented May 27 at 9:49
  • It kind of helped. It solved my problem, although my code is not so consistent and reusable now. But I still don't understand why does this problem occurs only with Twitter tags and not the Open graph tags.
    – Othan27
    Commented May 27 at 10:46
  • because as mentioned in NextJs docs "title, meta or any other elements (e.g. script) need to be contained as direct children of the Head element, or wrapped into maximum one level of <React.Fragment> or arrays—otherwise the tags won't be correctly picked up on client-side navigations." Commented May 27 at 11:33

0

Browse other questions tagged or ask your own question.