How to create a correct robots.txt file in GatsbyJS

Oscar de la Hera Gomez
Two flowers that represent GatsbyJS and Robots. Beneath them sits the text "Robots.txt."

Use the gatsby-plugin-robots-txt along with our guide to submit a valid robot.txt file.

A screenshot of PageSpeed Insights that shows that I wrote a wrong robots.txt file.

The following article walks you through how to create a correct robots.txt file using GatsbyJS and the gatsby-plugin-robots-txt plugin that produces no errors and which elevates your SEO.

Step One: Add the gatsby-plugin-robots-txt plugin

A screenshot of GatsbyJS's webpage for the gatsby-plugin-robots.txt

In Terminal, set the current directory to that of your GatsbyJS project and add the dependency by running the following line:

yarn add gatsby-plugin-robots-txt

The gatsby-plugin-robots-txt plugin will create a robots.txt at the right location within your website.

Step Two: Configure the plugin

A screenshot of VSCode showing the configuration that we used for the robots.txt plugin.

In the gatsby-config.js, add a configuration for the gatsby-plugin-robots.txt similar to the one below.

Please note that you need to update the sitemap url to point to the correct URL.

To learn alternative ways for writing valid robots.txt rules consult the article below.

For an example of using two user-agents, please consult the snippet below. The same pattern can be repeated to add more user-agents.

Step Three: Test

A screenshot of PageSpeed Insights showing that we resolved the robots.txt issue.

Build and deploy your website and confirm that PageSpeed Insights gives no robots.txt error.

Looking to learn more about ReactJS, GatsbyJS or SEO ?

Search our blog to find educational content on learning SEO as well as how to use ReactJS and GatsbyJS.

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.
delasign logo

Book a Free Consultation.

An icon of an email.

Click here to email us.

Fill in the details below to book a free consultation or to let us know about something else. Whatever it is, we are here to help.

How can we help you ?

Contact Details