-
DescriptionI have been tinkering with my own website's SEO, and I want to make sure that for some pages (such as Home, About, Portfolio and Blog pages) in my website have Twitter card images. I tried to import it locally from Here is my import React from 'react';
import Helmet from 'react-helmet';
import PropTypes from 'prop-types';
import { StaticQuery, graphql } from 'gatsby';
import ogImage from "../../content/assets/metaImage.png"
const query = graphql`
query getSiteMetadata {
site {
siteMetadata {
author
description
keywords
siteUrl
title
}
}
}
`;
function SEO({ meta, title, description, slug, lang, type }) {
return(
<StaticQuery
query = {query}
render = {data => {
const { siteMetadata } = data.site;
const metaDescription = description || siteMetadata.description;
const defaultTitle = data.site.siteMetadata?.title;
const url = `${siteMetadata.siteUrl}${slug}`;
const keywords = siteMetadata.keywords;
return(
<Helmet
htmlAttributes = {{lang, }}
title={title}
titleTemplate={defaultTitle ? `%s - ` + data.site.siteMetadata.author : null}
meta={
[
{
name: 'description',
content: metaDescription,
},
{
property: 'og:url',
content: url,
},
{
property: 'og:title',
content: title || siteMetadata.title,
},
{
property: 'og:description',
content: metaDescription,
},
{
name: 'twitter:card',
content: `summary_large_image`,
},
{
name: 'twitter:creator',
content: data.site.siteMetadata.title,
},
{
name: 'twitter:title',
content: title || siteMetadata.title,
},
{
name: 'twitter:description',
content: metaDescription,
},
]
.concat(
keywords.length > 0
?
{
content: keywords.filter((k) => typeof k === 'string').join(`, `),
name: `keywords`,
}
: []
)
.concat(
type === "website"
?
[
{
property: `og:type`,
content: `website`,
},
{
property: 'og:image',
content: ogImage,
},
{
name: 'twitter:image',
content: ogImage,
},
]
:
[
{
property: `og:type`,
content: type
},
{
property: 'og:image',
content: `${url}twitter-card.jpg`,
},
{
name: 'twitter:image',
content: `${url}twitter-card.jpg`,
},
]
)
.concat(meta)
}
/>
)
}}
/>
);
};
SEO.defaultProps = {
lang: `en`,
meta: [],
title: '',
slug: '',
};
SEO.propTypes = {
description: PropTypes.string,
lang: PropTypes.string,
meta: PropTypes.arrayOf(PropTypes.object),
slug: PropTypes.string,
title: PropTypes.string.isRequired,
};
export default SEO; Actual resultEnvironment
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Have you tried adding a full image URL? Your current setup is this:
It likely needs to be this:
I am not 100% sure about it but the validator returns |
Beta Was this translation helpful? Give feedback.
-
I'm facing the same issue, My seo.js is as below import React from "react"
import { Helmet } from "react-helmet"
import { useStaticQuery, graphql } from "gatsby"
const SEO = ({
title:blogTitle,
description: blogDescription,
image: blogImage,
pathname,
article
}) => {
const { site } = useStaticQuery(
graphql`
query {
site {
siteMetadata {
defaultTitle: title
defaultDescription: description
author {
name
summary
}
titleTemplate
defaultImage: image
siteUrl
keywords
social {
twitter
instagram
linkedin
}
}
}
}
`
)
const {
defaultTitle,
defaultDescription,
siteUrl,
social,
titleTemplate,
} = site.siteMetadata
const { twitter } = social
const title = blogTitle || defaultTitle;
const image = blogImage?.src ? siteUrl + blogImage.src : siteUrl + "/jayGurav.jpg";
const description = blogDescription || defaultDescription;
const url = pathname ? siteUrl + pathname : siteUrl;
return (
<Helmet
title={title}
titleTemplate={titleTemplate}
htmlAttributes={{ lang: "eng" }}
meta={[
{ name:"description", content: description },
{ name:"image", content: image },
{ property:"og:url", content: url },
{ property:"og:type", content: article ? "article" : "website" },
{ property:"og:title", content: title },
{ property:"og:description", content: description },
{ property: "og:image", content: image, },
{ name: "twitter:card", content: "summary_large_image" },
{ name: "twitter:domain", content: siteUrl },
{ name: "twitter:url", content: url },
{ name: "twitter:title", content: title },
{ name: "twitter:description", content: description },
{ name: "twitter:image", content: image },
{ name: "twitter:creator", content: twitter }
]}
link={[
{ rel:"canonical", href:url }
]}
/>
)
}
export default SEO As @vladar suggested I have also tried changing relative URLs to full URL, When I inspect my site all the meta tags seems accurate, I'm not sure what's going on... Twitter card validator shows an error as below |
Beta Was this translation helpful? Give feedback.
Have you tried adding a full image URL? Your current setup is this:
It likely needs to be this:
I am not 100% sure about it but the validator returns
twitter:image: Invalid URL
for your current format which suggests it might be the case.