2025.05.06

gatsby-robots-txt-seo

[Gatsby.js] gatsby-plugin-robots-txtでrobot.txtを生成する

  • Gatsby
  • SEO

Gatsby での robots.txt の設置は、検索エンジンのクロール制御という観点からSEOでは非常に重要です。

また適切にSitemap を指定することで、検索エンジンにサイト全体の構造を効率的に伝えることができます。

プラグインの利用

以下のプラグインを利用します。

npm install gatsby-plugin-robots-txt

設定

gatsby-config.jsで以下のように設定しました。

    siteMetadata: {
      siteUrl: `https://topaz-inc.dev/`,
    },
    
    //中略

    {
      resolve: "gatsby-plugin-robots-txt",
      options: ({ siteMetadata }) => ({
        host: siteMetadata.siteUrl,
        sitemap: `${siteMetadata.siteUrl}/sitemap-index.xml`,
        policy: [
          {
            userAgent: "*",
            allow: "/",
            disallow: ["/404", "/404.html", "/admin"],
          },
        ],
      }),
    },

ビルド

gatsby buildして以下のような、public/robots.txtが自動生成されていたら成功です。

User-agent: *
Allow: /
Sitemap: https://topaz-inc.dev/sitemap-index.xml
Host: https://topaz-inc.dev

もしstaging環境などを用意している場合などは、クロールできないようになど環境ごとの設定が追加で必要です。

以上、robots.txtはぜひsitemapと合わせて設置したいですね。