Añadir fichero robots.txt a Gatsby React

September 25th, 2021 | 2 min read | allow disallow gatsby-plugin-robots-txt robots.txt

Hola!,


En esta entrada vamos a ver cómo añadir el fichero robots a tu instalación Gatsby.

Para ello lo primero es instalar el plugin directamente desde node o desde yarn.


Puedes hacerlo lanzando uno de los siguientes comandos:

yarn add gatsby-plugin-robots-txt

o bien

npm install --save gatsby-plugin-robots-txt

Después basta con que lo añadas a tu fichero gatsby-config.js

module.exports = {
  plugins: [
    {
      resolve: 'gatsby-plugin-robots-txt',
      options: {
        configFile: 'robots-txt.config.js'
      }
    }
  ]
};

En mi caso voy a customizar este fichero porque necesito que algunas de las URL no las indexe Google , por ejemplo no quiero que indexe las URL /tag/

Tendriamos que crear el fichero robots-txt.config.js y añadir ahí la configuración del fichero.

La configuración que le he puesto en ese fichero sería la siguiente:

module.exports = {
    host: 'https://malditawifi.web.app',
    sitemap: 'https://malditawifi.web.app/sitemap.xml',
    policy: [
        {
            userAgent: "Googlebot",
            allow: "/",
            disallow: "/tag",
            crawlDelay: 2,
        },
        {
            userAgent: "*",
            allow: "/",
            disallow: "/tag",
            crawlDelay: 10,
            cleanParam: "ref /articles/",
        },
    ],
};

Y el resultado sería asi:

User-agent: Googlebot
Allow: /
Disallow: /tag/
Crawl-delay: 2

User-agent: *
Allow: /
Disallow: /tag/
Crawl-delay: 10
Clean-param: ref /articles/
Sitemap: https://malditawifi.web.app/sitemap.xml
Host: https://malditawifi.web.app

Una vez que realices el Build verás tu flamante fichero robots.txt generado como le has indicado.

Volver

Miguel, consultor y desarrollador web, devOps and warrior code XD