2025.05.19

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

Shuji
- 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と合わせて設置したいですね。
その他の記事
Other Articles
FastAPI × React LeafletでGoogle Earth Engineの衛星データを扱う
Google Earth Engine で衛生データを表示する
DifyのワークフローをNode Cronで定期実行
Difyの外部ナレッジAPIでNotion APIのデータを取得
Dify × NotionでAIチャットボットを構築する
Gatsby × Tailwindcss × ダークモードでSEO対策
[Gatsby.js] gatsby-plugin-canonical-urlsでcanonicalタグを生成する
[Gatsby.js] gatsby-plugin-sitemapでサイトマップを生成する
Difyのワークフローでスクレイピングしたデータを外部アプリへPOST
Difyの外部ナレッジAPIでRAGを拡張する
DifyとRemixの連携によるチャットボット構築
Remix × Vitest で Login API をテストする
GatsbyでGifを動作させる(TypeScript)
RemixでStripeを使った決済機能の導入