1
0
Fork 0
blog/src/components/atoms/SEO.jsx

206 lines
4.5 KiB
React
Raw Normal View History

2018-09-25 01:39:38 +02:00
import React from 'react'
2018-09-24 23:50:39 +02:00
import PropTypes from 'prop-types'
import { StaticQuery, graphql } from 'gatsby'
import Helmet from 'react-helmet'
const query = graphql`
query {
2018-09-30 03:11:08 +02:00
site {
siteMetadata {
siteTitle
siteDescription
siteUrl
author {
name
twitter
2018-10-01 19:57:49 +02:00
}
}
}
logo: allFile(filter: { name: { eq: "apple-touch-icon" } }) {
edges {
node {
relativePath
2018-09-24 23:50:39 +02:00
}
}
}
}
`
2018-09-25 01:39:38 +02:00
const createSchemaOrg = (
blogURL,
title,
siteMeta,
postSEO,
postURL,
image,
description
) => {
const schemaOrgJSONLD = [
{
'@context': 'http://schema.org',
'@type': 'WebSite',
url: blogURL,
name: title,
alternateName: siteMeta.titleAlt ? siteMeta.titleAlt : ''
}
]
2018-09-24 23:50:39 +02:00
2018-09-25 01:39:38 +02:00
if (postSEO) {
schemaOrgJSONLD.push(
{
'@context': 'http://schema.org',
'@type': 'BreadcrumbList',
itemListElement: [
{
'@type': 'ListItem',
position: 1,
item: {
'@id': postURL,
2018-09-24 23:50:39 +02:00
name: title,
2018-09-25 01:39:38 +02:00
image
2018-09-24 23:50:39 +02:00
}
}
2018-09-25 01:39:38 +02:00
]
},
{
'@context': 'http://schema.org',
'@type': 'BlogPosting',
url: blogURL,
name: title,
alternateName: siteMeta.titleAlt ? siteMeta.titleAlt : '',
headline: title,
image: {
'@type': 'ImageObject',
url: image
},
description
}
2018-09-24 23:50:39 +02:00
)
}
2018-09-25 01:39:38 +02:00
return schemaOrgJSONLD
}
const MetaTags = ({
description,
image,
url,
schema,
postSEO,
title,
siteMeta
}) => (
2018-09-27 21:14:22 +02:00
<Helmet
2018-10-01 20:02:31 +02:00
defaultTitle={`${siteMeta.siteTitle} ¦ ${siteMeta.siteDescription}`}
titleTemplate={`%s ¦ ${siteMeta.siteTitle}`}
2018-09-27 21:14:22 +02:00
>
2018-09-25 01:39:38 +02:00
<html lang="en" />
{/* General tags */}
<meta name="description" content={description} />
<meta name="image" content={image} />
<link rel="canonical" href={url} />
{/* Schema.org tags */}
<script type="application/ld+json">{schema}</script>
{/* OpenGraph tags */}
<meta property="og:url" content={url} />
{postSEO && <meta property="og:type" content="article" />}
<meta property="og:title" content={title} />
<meta property="og:description" content={description} />
<meta property="og:image" content={image} />
{/* Twitter Card tags */}
<meta name="twitter:card" content="summary_large_image" />
<meta
name="twitter:creator"
content={siteMeta.author.twitter ? siteMeta.author.twitter : ''}
/>
<meta name="twitter:title" content={title} />
<meta name="twitter:description" content={description} />
<meta name="twitter:image" content={image} />
2019-04-13 22:52:58 +02:00
<link
rel="alternate"
title="JSON Feed"
type="application/json"
href={`${siteMeta.siteUrl}/feed.json`}
/>
2018-09-25 01:39:38 +02:00
</Helmet>
)
MetaTags.propTypes = {
description: PropTypes.string,
image: PropTypes.string,
url: PropTypes.string,
schema: PropTypes.string,
postSEO: PropTypes.bool,
title: PropTypes.string,
siteMeta: PropTypes.object
}
const SEO = ({ post, slug, postSEO }) => (
<StaticQuery
query={query}
render={data => {
2018-09-30 03:11:08 +02:00
const siteMeta = data.site.siteMetadata
2018-10-01 19:57:49 +02:00
const logo = data.logo.edges[0].node.relativePath
2018-09-25 01:39:38 +02:00
let title
let description
let image
let postURL
if (postSEO) {
const postMeta = post.frontmatter
2018-10-24 15:05:35 +02:00
title = `${postMeta.title} ¦ ${siteMeta.siteTitle}`
2018-09-25 01:39:38 +02:00
description = postMeta.description ? postMeta.description : post.excerpt
2018-11-07 01:15:41 +01:00
image = postMeta.image
? postMeta.image.childImageSharp.fluid.src
: `/${logo}`
2018-09-30 03:11:08 +02:00
postURL = `${siteMeta.siteUrl}${slug}`
2018-09-25 01:39:38 +02:00
} else {
2018-09-30 03:11:08 +02:00
title = `${siteMeta.siteTitle} ¦ ${siteMeta.siteDescription}`
description = siteMeta.siteDescription
2018-11-07 01:15:41 +01:00
image = `/${logo}`
2018-09-25 01:39:38 +02:00
}
2018-09-30 03:11:08 +02:00
image = `${siteMeta.siteUrl}${image}`
const blogURL = siteMeta.siteUrl
2018-09-25 01:39:38 +02:00
const url = postSEO ? postURL : blogURL
let schema = createSchemaOrg(
blogURL,
title,
siteMeta,
postSEO,
postURL,
image,
description
)
schema = JSON.stringify(schema)
return (
<MetaTags
description={description}
image={image}
url={url}
schema={schema}
postSEO={postSEO}
title={title}
siteMeta={siteMeta}
/>
)
}}
/>
)
SEO.propTypes = {
post: PropTypes.object,
slug: PropTypes.string,
postSEO: PropTypes.bool
2018-09-24 23:50:39 +02:00
}
export default SEO