How To Do Meta Tags In GatsbyJS

How to set all your meta tags with a new GastbyJS website

Manny
yellowcode
7 min readJan 29, 2019

--

How to set all your meta tags with a new GastbyJS website

What is Gatsby?

I like to think of GatsbyJS as the new kid on the block that is allowing you to do static websites in React. It’s definitely a mind shift from traditional coding in HTML and CSS but it does offer some significant benefits.

GatsbyJS.org

Why Are Meta Tags Still Import?

Meta tags are still widely used among websites. They are entirely used for their indexing as they once were but they are more so used now on how they are displayed when sharing them across different social media platforms to gain attention.

Facebook URL Linter

Hence meta tags are important.

Starting A New Project

We’ll start by creating a new project with Gatsby and creating a simple website.

You will need to make sure you have the latest version of NodeJS installed on your local computer.

Next you’ll want to install Gatsby CLI.

Scaffolding Gatsby Starter

After you’ve install Gatsby CLI, we’ll run the script to scaffold out the hello-world project.

It’ll install its dependencies and we should be good to go.

Running Development Environment

For the purposes of this tutorial, I’ll be using yarn instead of npm but you can definitely use latter as well.

Gatsby Hello-World Running Locally

Creating Some HTML

Let’s give the page some HTML and take a look what’s inside our main file, /src/pages/index.js:

src/pages/index.js

Right away you’ll notice that there aren’t any <head> tags for you to insert your <meta> tags. That’s because GatsbyJS manages everything, so we’ll need a way around this. But first:

/src/pages/index.js

Our website already looking amazing.

React Helmet & Gatsby Plugin

Link: react-helmet

So how do we add the meta tags? With react-helmet and gatsby-plugin-react-helmet we can achieve this.

We need to add one configuration to our repository / project in the root.

gatsby-config.js

Let’s add a simple title tag to start.

src/pages/index.js:

See out new <title> tag working.

Ok, now that we’re able to add tags to the header, let’s go full out for the meta tags for all the social media, but also for the the favicon and mobile too.

MetaTags.io

Link: MetaTags.io

To start, we’re going to go to metatags.io and fill in the content on the website. This is a great tool created by Moe Amaya and I definitely recommend it.

My Awesome Website Meta Data

If you’re wondering what size of graphic I’m using here, the size is 1500px by 780px as a png. You download MetaTags.io sample one from here:

https://metatags.io/assets/meta-tags-16a33a6a8531e519cc0936fbba0ad904e52d35f34a46c97a2c9f6f7dd7d336f2.png

Next, we copy the necessary meta tags to our code by clicking Generate Meta Tags in the top right of the website.

Generated Meta Tags

Back in our code, we’ll add the code into our component and comment out the <!-- html comments --> as JSX comments.

NOTE: Make sure to add closing tags at the end of our meta tags as React does not like unclosed tags.

/src/pages/index.js

Make sure to specify the exact route for your facebook and twitter images.

You’ll need to put those files in your static folder in order for it to be accessible:

Refactoring

There seems to be a lot of tags there, so I think we’ll split this up into its own component and pull it in as needed. Create a new folder called /src/shared and create a new component called Head.js in it.

/src/shared/Head.js

Now we can add that component to the main component:

/src/pages/index.js

Looks prettier.

FavicoMatic.com

Link: Favicomatic

Next, we’ll recreate our favicon and use a tool called FavicoMatic to generate all the different sizes for our favicon to be supported on different devices.

https://favicomatic.com

You’ll want to make sure you adhere to the graphic requirements and once you upload your image it will spit out a zip file for you to download and put in your static folder.

Code generated

Let’s copy that code into our Head.js:

/src/shared/Head.js

Voilà!

We now have all the meta tags needed for social media and more. You can even check the Facebook Lint Debugger to see if the meta tags you put are being parsed properly. Here’s an example I did with yellowcode.co:

Facebook Lint Debugging

If you have any recommendations on how this can be implemented better or talk about coding/algorithms, I would love to chat.

If you got value from this, please share it on twitter 🐦 or other social media platforms. Thanks again for reading. 🙏

Please also follow me on twitter: @codingwithmanny and instagram at @codingwithmanny.

--

--

Manny
yellowcode

DevRel Engineer @ Berachain | Prev Polygon | Ankr & Web Application / Full Stack Developer