Skip to content
This repository has been archived by the owner on Mar 11, 2024. It is now read-only.

Commit

Permalink
Merge branch 'seo-component'
Browse files Browse the repository at this point in the history
  • Loading branch information
bagseye committed Aug 15, 2020
2 parents aa86999 + ed454f5 commit b2da6aa
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 61 deletions.
3 changes: 2 additions & 1 deletion gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ module.exports = {
siteMetadata: {
title: `Bonneville`,
titleTemplate: `%s | A starter theme for Gatsby`,
siteUrl: `https://bonneville.netlify.com`,
description: `A starter theme for Gatsby`,
siteUrl: `https://bonneville.netlify.com`,
image: `/images/color.jpg`,
author: `Morgan Baker`,
authorSite: `https://www.morganbaker.dev`,
twitterUsername: `@bonneville`,
Expand Down
127 changes: 67 additions & 60 deletions src/components/SEO.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,74 @@
import React from "react"
import { Helmet } from "react-helmet"
import PropTypes from "prop-types"
import { StaticQuery, graphql } from "gatsby"
import { useLocation } from "@reach/router"
import { useStaticQuery, graphql } from "gatsby"

const SEO = ({ title, description, lang, image, article }) => {
const { pathname } = useLocation()
const { site } = useStaticQuery(query)

const {
defaultTitle,
titleTemplate,
defaultDescription,
siteUrl,
defaultImage,
twitterUsername,
} = site.siteMetadata

const seo = {
title: title || defaultTitle,
description: description || defaultDescription,
image: `${siteUrl}${image || defaultImage}`,
url: `${siteUrl}${pathname}`,
}

return (
<Helmet
htmlAttributes={{ lang }}
title={seo.title}
titleTemplate={titleTemplate}
>
<meta name="description" content={seo.description} />
<meta name="image" content={seo.image} />
{seo.url && <meta property="og:url" content={seo.url} />}
{(article ? true : null) && <meta property="og:type" content="article" />}
{seo.title && <meta property="og:title" content={seo.title} />}
{seo.description && (
<meta property="og:description" content={seo.description} />
)}
{seo.image && <meta property="og:image" content={seo.image} />}
<meta name="twitter:card" content="summary_large_image" />
{twitterUsername && (
<meta name="twitter:creator" content={twitterUsername} />
)}
{seo.title && <meta name="twitter:title" content={seo.title} />}
{seo.description && (
<meta name="twitter:description" content={seo.description} />
)}
{seo.image && <meta name="twitter:image" content={seo.image} />}
</Helmet>
)
}

const SEO = ({ title, description, image, pathname, article }) => (
<StaticQuery
query={query}
render={({
site: {
siteMetadata: {
defaultTitle,
titleTemplate,
defaultDescription,
siteUrl,
twitterUsername,
},
},
}) => {
const seo = {
title: title || defaultTitle,
description: description || defaultDescription,
url: `${siteUrl}${pathname || "/"}`,
}
return (
<>
<Helmet title={seo.title} titleTemplate={titleTemplate}>
<meta name="description" content={seo.description} />
{seo.url && <meta property="og:url" content={seo.url} />}
{(article ? true : null) && (
<meta property="og:type" content="article" />
)}
{seo.title && <meta property="og:title" content={seo.title} />}
{seo.description && (
<meta property="og:description" content={seo.description} />
)}
<meta name="twitter:card" content="summary_large_image" />
{twitterUsername && (
<meta name="twitter:creator" content={twitterUsername} />
)}
{seo.title && <meta name="twitter:title" content={seo.title} />}
{seo.description && (
<meta name="twitter:description" content={seo.description} />
)}
</Helmet>
</>
)
}}
/>
)
export default SEO

SEO.propTypes = {
title: PropTypes.string,
description: PropTypes.string,
image: PropTypes.string,
article: PropTypes.bool,
}

SEO.defaultProps = {
title: null,
description: null,
image: null,
lang: `en`,
article: false,
}

const query = graphql`
query SEO {
site {
Expand All @@ -57,22 +77,9 @@ const query = graphql`
titleTemplate
defaultDescription: description
siteUrl
defaultImage: image
twitterUsername
}
}
}
`
SEO.propTypes = {
title: PropTypes.string,
description: PropTypes.string,
image: PropTypes.string,
pathname: PropTypes.string,
article: PropTypes.bool,
}
SEO.defaultProps = {
title: null,
description: null,
image: null,
pathname: null,
article: false,
}
Binary file added src/images/color.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b2da6aa

Please sign in to comment.