GatsbyJS Series — SEO basic for blog (Part 6)

Jun 26, 2019

In this part, we’ll add some basic SEO information for our blog then adding some configurations to Netlify service so that our blog can work correctly when deploy to production. Finally, we’ll add a custom domain (namecheap) for our blog.

SEO improvement

First, we’ll create some basic blog information in gatsby-config.js:

// previous code

module.exports = {
  siteMetadata: {
    title: `Mr Leo`,
    author: `Mr Leo`,
    description: `Mr Leo blog and portfolio`,
    siteUrl: `https://www.mrleo.dev`,
  },
  plugins: [
    // configurations
  ],
}

react-helmet

It is a React component will manage all of your changes to the document head.

yarn add react-helmet gatsby-plugin-react-helmet
yarn add -D @types/react-helmet

Add gatsby-plugin-react-helmet to gatsby-config.js as well:

// previous code

module.exports = {
  // previous code
  plugins: [
    // previous configurations
    `gatsby-plugin-react-helmet`,
  ],
}

SEO component

Next, create a SEO component inside src/components/:

/**
 * SEO component that queries for data with
 *  Gatsby's useStaticQuery React hook
 *
 * See: https://www.gatsbyjs.org/docs/use-static-query/
 */

import React from 'react'
import Helmet from 'react-helmet'
import { useStaticQuery, graphql } from 'gatsby'

interface IProps {
  title: string
  description?: string
  lang?: any
  meta?: any
}

const SEO = (props: IProps) => {
  const { description = ``, lang = `en`, meta = [], title } = props
  const { site } = useStaticQuery(
    graphql`
      query {
        site {
          siteMetadata {
            title
            description
            author
          }
        }
      }
    `
  )

  const metaDescription = description || site.siteMetadata.description

  return (
    <Helmet
      htmlAttributes={{
        lang,
      }}
      title={title}
      titleTemplate={`%s | ${site.siteMetadata.title}`}
      meta={[
        {
          name: `description`,
          content: metaDescription,
        },
        {
          property: `og:title`,
          content: title,
        },
        {
          property: `og:description`,
          content: metaDescription,
        },
        {
          property: `og:type`,
          content: `website`,
        },
        {
          name: `twitter:card`,
          content: `summary`,
        },
        {
          name: `twitter:creator`,
          content: site.siteMetadata.author,
        },
        {
          name: `twitter:title`,
          content: title,
        },
        {
          name: `twitter:description`,
          content: metaDescription,
        },
      ].concat(meta)}
    />
  )
}

export default SEO

Inject Seo component into home page and blog template:

home page
src/pages/index.tsx

blog post template
src/templates/blog-post.tsx

Now you’ll see the nice titles for home page and each post:

Home page title
Home page title

Blog details title
Blog details title

Try to inspect DOM we can see seo information in our header as well:

SEO information in header
SEO information in header

Add Contentful APIs to Netlify

We have done a lot of work. Time to add deployment APIs to netlify so that our blog can go live successfully.

Go to Settings in app.netlify.com dashboard:

Settings
Settings

Select Environment under Build & deploy menu. Click on Edit variables to add new:

Environment variables
Environment variables

The values get from Contentful APIs we mentioned in the previous part. Save it and commit all our code. Waiting for Netlify deploy our blog in few minutes then check the result.

Go live
Go live

Add custom domain

Instead using a random name domain generated by Netlify, we can add our custom domain.

Netlify Domain Settings

Login to Netlify dashboard admin follow these steps: Select Settings -> Domain management -> Add custom domain

Add custom domain
Add custom domain

Input our domain and verify it
Input our domain and verify it

Enable DNS configuration

Verify DNS configuration
Verify DNS configuration

Check DNS configuration

Check DNS configuration
Check DNS configuration

Optain IP address
Optain IP address

Namecheap configuration

Login to namecheap account. Select our domain in list then select Advance DNS menu:

Domain List
Domain List

Follow these configurations:

Follow these following configurations
Follow these following configurations

  1. Remove URL redirect record
  2. Update value for CNAME Record to Netlify URL. In our case is vibrant-mcclintock-5a849d.netlify.com
  3. Add an A Record with host is @ and value is 104.198.14.52 (IP we obtained from Netlify above).

Ok, this configuration will take us several minutes to apply. Time for a cup of coffee and waiting for our domain applied.

Summary

We have done a lot of work so far. At this moment, we can build own blog from scratch with powerful support from GatsbyJS framework.

In the future, we can add more advanced features for our blogs: comment, contact form, sidebar menu, etc.