Gle*_*len 2 javascript reactjs google-rich-snippets gatsby
我有一个 Gatsby 的基本博客设置,在发布此问题时,缺少 SEO 组件的良好文档。有一些基本 SEO 组件的示例,但我想要的是更深入一些。也许,如果在这里达成了解决方案,则可以将其贡献给 Gatsby 文档,让其他人受益。
在通常的标题和描述元标记以及 facebook/twitter 开放图元(我已经完成)之上,我想为丰富的片段添加结构化数据,这些数据将根据博客文章的类型而有所不同。例如,我可能有一个常规帖子会打印文章架构,有些帖子可能是How-to,在这种情况下,我想打印 HowTo 架构而不是文章。在某些时候,我可能会写一篇适合常见问题解答架构的帖子。
我不知道这是否是最好的方法,但这是我的想法:
我也在考虑将架构数据存储在 frontmatter 中,但由于这些数据非常复杂,并且会因帖子类型而异(文章、方法等),我不确定这是否是一个好主意?
---
title: Hello World
description: How to say hello
article: false
how-to: true
faq: false
---
Run Code Online (Sandbox Code Playgroud)
下面是我的整个 SEO 组件,这显然不起作用,但您希望能看到我的想法。我已经剖析并借鉴了gatsby 高级启动组件和gatsby 启动棱镜组件,但都没有完全满足我的需求。这是我的:
import React from "react"
import Helmet from "react-helmet"
import PropTypes from "prop-types"
import { useStaticQuery, graphql } from "gatsby"
import Facebook from "./Facebook"
import Twitter from "./Twitter"
const SEO = ({
title,
desc,
banner,
pathname,
published,
modified,
article,
webpage,
node,
}) => {
const { site } = useStaticQuery(query)
const {
buildTime,
siteMetadata: {
siteUrl,
defaultTitle,
defaultDescription,
defaultBanner,
headline,
siteLanguage,
ogLanguage,
author,
twitter,
facebook,
},
} = site
const seo = {
title: title || defaultTitle,
description: desc || defaultDescription,
image: `${siteUrl}${banner || defaultBanner}`,
url: `${siteUrl}${pathname || "/"}`,
date_published: published,
date_modified: modified,
}
// Default Website Schema
const schemaOrgJSONLD = [
{
"@context": "http://schema.org",
"@type": "WebSite",
url: siteUrl,
name: defaultTitle,
alternateName: headline ? headline : "",
},
]
if (howto) {
schemaOrgJSONLD.push({
/* HowTo Schema here */
})
}
if (faq) {
schemaOrgJSONLD.push({
/* FAQ Schema here */
})
}
if (article) {
schemaOrgJSONLD.push({
/* Regular Article Schema */
"@context": "http://schema.org",
"@type": "Article",
author: {
"@type": "Person",
name: author,
},
copyrightHolder: {
"@type": "Person",
name: author,
},
copyrightYear: "2019",
creator: {
"@type": "Person",
name: author,
},
publisher: {
"@type": "Organization",
name: author,
logo: {
"@type": "ImageObject",
url: `${siteUrl}${defaultBanner}`,
},
},
datePublished: seo.date_published,
dateModified: seo.date_modified,
description: seo.description,
headline: seo.title,
inLanguage: siteLanguage,
url: seo.url,
name: seo.title,
image: {
"@type": "ImageObject",
url: seo.image,
},
mainEntityOfPage: seo.url,
})
}
return (
<>
<Helmet title={seo.title}>
<html lang={siteLanguage} />
<meta name="description" content={seo.description} />
<meta name="image" content={seo.image} />
{/* Schema.org tags */}
<script type="application/ld+json">
{JSON.stringify(schemaOrgJSONLD)}
</script>
</Helmet>
<Facebook
desc={seo.description}
image={seo.image}
title={seo.title}
type={article ? "article" : "website"}
url={seo.url}
locale={ogLanguage}
name={facebook}
/>
<Twitter
title={seo.title}
image={seo.image}
desc={seo.description}
username={twitter}
/>
</>
)
}
export default SEO
SEO.propTypes = {
title: PropTypes.string,
desc: PropTypes.string,
banner: PropTypes.string,
pathname: PropTypes.string,
published: PropTypes.string,
modified: PropTypes.string,
article: PropTypes.bool,
webpage: PropTypes.bool,
node: PropTypes.object,
}
SEO.defaultProps = {
title: null,
desc: null,
banner: null,
pathname: null,
published: null,
modified: null,
article: false,
webpage: false,
node: null,
}
const query = graphql`
query SEO {
site {
buildTime(formatString: "YYYY-MM-DD")
siteMetadata {
siteUrl
defaultTitle: title
defaultDescription: description
defaultBanner: logo
headline
siteLanguage
ogLanguage
author
logo
twitter
facebook
}
}
}
`
Run Code Online (Sandbox Code Playgroud)
正面:
---
type: howto // Use either 'article' or 'howto'
---
Run Code Online (Sandbox Code Playgroud)
像查询其他数据一样使用 GraphQL 查询它:
frontmatter {
title
published(formatString: "MMMM DD, YYYY")
modified(formatString: "MMMM DD, YYYY")
description
type
}
Run Code Online (Sandbox Code Playgroud)
将其传递给您的 SEO 组件:
---
type: howto // Use either 'article' or 'howto'
---
Run Code Online (Sandbox Code Playgroud)
在您的 SEO 组件中,您可以像这样使用它(对所有类型都这样做)。您可以根据需要为我的类型、常见问题解答、课程等设置您的帖子和 SEO 组件:
frontmatter {
title
published(formatString: "MMMM DD, YYYY")
modified(formatString: "MMMM DD, YYYY")
description
type
}
Run Code Online (Sandbox Code Playgroud)
最后,在 React Helmet 中,我们有:
<SEO
title={post.frontmatter.title}
desc={post.frontmatter.description}
published={post.frontmatter.published}
modified={post.frontmatter.modified}
type={post.frontmatter.type}
/>
Run Code Online (Sandbox Code Playgroud)