Skip to content

Instantly share code, notes, and snippets.

@belachkar
Created March 4, 2021 20:33
Show Gist options
  • Save belachkar/1b7b45f6c174b59863ccf66a3fe17e91 to your computer and use it in GitHub Desktop.
Save belachkar/1b7b45f6c174b59863ccf66a3fe17e91 to your computer and use it in GitHub Desktop.

React SEO

SEO.js

import React from "react";
import PropTypes from "prop-types";
import { Helmet } from "react-helmet";
import { useStaticQuery, graphql } from "gatsby";

const SEO = ({ title, description }) => {
  const {
    site: {
      siteMetadata: {
        author,
        siteDesc,
        image,
        siteUrl,
        siteTitle,
        twitterUsername,
      },
    },
  } = useStaticQuery(query);

  const imgURL = `${siteUrl}${image}`;
  const fullTitle = title ? `${title} | ${siteTitle}` : siteTitle;

  return (
    <Helmet htmlAttributes={{ lang: "en" }} title={fullTitle}>
      <meta name="description" content={description || siteDesc} />
      <meta name="image" content={image} />
      {/* Twitter card */}
      <meta name="twitter:card" content="summary_large_image" />
      <meta name="twitter:site" content={twitterUsername} />
      <meta name="twitter:creator" content={twitterUsername} />
      <meta property="og:title" content={siteTitle} />
      <meta property="og:description" content={siteDesc} />
      <meta property="og:image" content={imgURL} />
      {/* <meta name="twitter:title" content={siteTitle} />
      <meta name="twitter:description" content={siteDesc} />
      <meta name="twitter:image" content={imgURL} /> */}
    </Helmet>
  );
};

const query = graphql`
  {
    site {
      siteMetadata {
        author
        siteDesc: description
        image
        siteUrl
        siteTitle: title
        twitterUsername
      }
    }
  }
`;

SEO.propTypes = {
  title: PropTypes.string.isRequired,
  description: PropTypes.string.isRequired,
};

export default SEO;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment