1.8.0 • Published 1 year ago

gatsby-plugin-robots-txt v1.8.0

Weekly downloads
84,879
License
MIT
Repository
github
Last release
1 year ago

NPM version Actions Build Status Reliability Rating Coverage FOSSA Status

gatsby-plugin-robots-txt

Create robots.txt for your Gatsby site on build.

Install

yarn add gatsby-plugin-robots-txt

or

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

How To Use

gatsby-config.js

module.exports = {
  siteMetadata: {
    siteUrl: 'https://www.example.com'
  },
  plugins: ['gatsby-plugin-robots-txt']
};

Options

This plugin uses generate-robotstxt to generate content of robots.txt and it has the following options:

NameTypeDefaultDescription
hostString${siteMetadata.siteUrl}Host of your site
sitemapString / String[]${siteMetadata.siteUrl}/sitemap/sitemap-index.xmlPath(s) to sitemap.xml
policyPolicy[][]List of Policy rules
configFileStringundefinedPath to external config file
outputString/robots.txtPath where to create the robots.txt

gatsby-config.js

module.exports = {
  plugins: [
    {
      resolve: 'gatsby-plugin-robots-txt',
      options: {
        host: 'https://www.example.com',
        sitemap: 'https://www.example.com/sitemap.xml',
        policy: [{userAgent: '*', allow: '/'}]
      }
    }
  ]
};

env-option

gatsby-config.js

module.exports = {
  plugins: [
    {
      resolve: 'gatsby-plugin-robots-txt',
      options: {
        host: 'https://www.example.com',
        sitemap: 'https://www.example.com/sitemap.xml',
        env: {
          development: {
            policy: [{userAgent: '*', disallow: ['/']}]
          },
          production: {
            policy: [{userAgent: '*', allow: '/'}]
          }
        }
      }
    }
  ]
};

The env key will be taken from process.env.GATSBY_ACTIVE_ENV first ( see Gatsby Environment Variables for more information on this variable), falling back to process.env.NODE_ENV. When this is not available then it defaults to development.

You can resolve the env key by using resolveEnv function:

gatsby-config.js

module.exports = {
  plugins: [
    {
      resolve: 'gatsby-plugin-robots-txt',
      options: {
        host: 'https://www.example.com',
        sitemap: 'https://www.example.com/sitemap.xml',
        resolveEnv: () => process.env.GATSBY_ENV,
        env: {
          development: {
            policy: [{userAgent: '*', disallow: ['/']}]
          },
          production: {
            policy: [{userAgent: '*', allow: '/'}]
          }
        }
      }
    }
  ]
};

configFile-option

You can use the configFile option to set specific external configuration:

gatsby-config.js

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

robots-txt.config.js

module.exports = {
  host: 'https://www.example.com',
  sitemap: 'https://www.example.com/sitemap.xml',
  policy: [{userAgent: '*'}]
};

Netlify

If you would like to disable crawlers for deploy-previews you can use the following snippet:

gatsby-config.js

const {
  NODE_ENV,
  URL: NETLIFY_SITE_URL = 'https://www.example.com',
  DEPLOY_PRIME_URL: NETLIFY_DEPLOY_URL = NETLIFY_SITE_URL,
  CONTEXT: NETLIFY_ENV = NODE_ENV
} = process.env;
const isNetlifyProduction = NETLIFY_ENV === 'production';
const siteUrl = isNetlifyProduction ? NETLIFY_SITE_URL : NETLIFY_DEPLOY_URL;

module.exports = {
  siteMetadata: {
    siteUrl
  },
  plugins: [
    {
      resolve: 'gatsby-plugin-robots-txt',
      options: {
        resolveEnv: () => NETLIFY_ENV,
        env: {
          production: {
            policy: [{userAgent: '*'}]
          },
          'branch-deploy': {
            policy: [{userAgent: '*', disallow: ['/']}],
            sitemap: null,
            host: null
          },
          'deploy-preview': {
            policy: [{userAgent: '*', disallow: ['/']}],
            sitemap: null,
            host: null
          }
        }
      }
    }
  ]
};

query-option

By default the site URL will come from the Gatsby node site.siteMeta.siteUrl. Like in Gatsby's sitemap plugin an optional GraphQL query can be used to provide a different value from another data source as long as it returns the same shape:

gatsby-config.js

module.exports = {
  plugins: [
    {
      resolve: 'gatsby-plugin-robots-txt',
      options: {
        query: `{
          site: MyCustomDataSource {
            siteMetadata {
              siteUrl
            }
          }
        }`
      }
    }
  ]
};

License

FOSSA Status

gatsby-theme-sanity-evelan@cangir/gatsby-theme-phoenixsomoy-portfolio@dewen_li/gatsby-theme-bodilessgatsby-theme-kuworking-landing-onegatsby-theme-memoriestorichegatsby-theme-factlytheme-gatsby-shopifyfriday.de@afuh/gatsby-theme-up42@up42/gatsby-theme-up42@garytee/gatsby-woo-elementorgatsby-woocommerce-elementor-theme@leptest/gatsby-netlify-cmstest-theme-shawacademygatsby-starter-essentials@hesburgh-wse/gatsby-theme-marble@rodriguesmyron/gatsby-theme-podcastgumroad-api-tester-webappgatsby-theme-rayriffy-blogpersonal-site@everything-registry/sub-chunk-1722@g.vocale/gatsby-theme-memoriestoriche@gatsbystorefront/gatsby-theme-storefront-shopify@fox-tech/doc@julien76/gatsby-theme-jhe@laradevitt/gatsby-theme-just-basics@lmack/core@hpprc/gatsby-theme-blog@hpprc/gatsby-theme-core@giraldomac/gatsby-theme-mindmerge@giraldomac/gatsby-theme-mmdbase@giraldomac/gatsby-theme-wvumedicine@hummingbird/gatsby-themeelinas-portfolio-gatsby@novvum/gatsby-theme-wikigatsby-starter-heydaysgatsby-starter-hoodiegatsby-starter-lumengatsby-starter-zoomkodinggatsby-starter-datocmsgatsby-theme-41vfhvd5dcwok96m8visgatsby-theme-academicgatsby-theme-academic-modifiedgatsby-theme-awesomenessgatsby-theme-catalyst-coregatsby-theme-buzzinggatsby-theme-bootstrapgatsby-theme-benrobertsongatsby-portfolio-themegatsby-theme-keplergatsby-theme-kuworking-affiliategatsby-theme-kuworking-coregatsby-theme-kuworking-landing-twogatsby-theme-mdx-suite-basegatsby-theme-julesgatsby-theme-mosmangatsby-theme-municipiogatsby-theme-narrabeengatsby-theme-hyperobjekt-coregatsby-theme-datocmsgatsby-theme-esca-boilerplategatsby-theme-core-testgatsby-theme-core-gwanggatsby-theme-octahedroidgatsby-theme-q3gatsby-theme-randwick@ntnhan30dh/gatsby-theme-wp-parent@opensourcerepos/gatsby-theme-opensourcerepos@opensourcerepos/opensourcerepos-theme@mhjadav/gatsby-theme-factly@mdrt/mdrt-theme-recruitmentgatsby-all-pack-theme-startergatsby-theme-sputnikgatsby-theme-stoutlabs-configsgatsby-theme-webhart-basegatsby-theme-wild-childgatsby-theme-wvumedicinegatsby-theme-portfolio-minimalgatsby-woocommerce-theme@newrelic/gatsby-theme-newrelic@kuworking/gatsby-theme-kuworking-core@riencoertjens/gatsby-theme-webhart-base@dungle-scrubs/gatsby-shopify-theme@packdigital/gatsby-theme-ripperoni@packdigital/gatsby-theme-ripperoni-marketing@eshlox/gatsby-theme-axii@sorrel340/gatsby-theme-portfolio-minimal@shenhh/smooth-docjonasbroms-www@syfxlin/gatsby-theme-coline@thepolicylab-projectportals/gatsby-theme-project-portal@foudroyer/honey@foudroyer/mur@foudroyer/purity@strv/gatsby-theme-core@benrobertson/gatsby-theme-seo@bodiless/gatsby-theme-bodiless@bond-london/gatsby-theme@campj/core
1.8.0

1 year ago

1.7.1

2 years ago

1.7.0

2 years ago

1.6.14

2 years ago

1.6.11

3 years ago

1.6.13

3 years ago

1.6.12

3 years ago

1.6.10

3 years ago

1.6.9

3 years ago

1.6.4

3 years ago

1.6.3

3 years ago

1.6.8

3 years ago

1.6.7

3 years ago

1.6.6

3 years ago

1.6.5

3 years ago

1.6.2

3 years ago

1.6.1

3 years ago

1.6.0

3 years ago

1.5.6

3 years ago

1.5.5

3 years ago

1.5.4

3 years ago

1.5.3

4 years ago

1.5.2

4 years ago

1.5.1

4 years ago

1.5.0

5 years ago

1.4.0

5 years ago

1.3.0

6 years ago

1.2.0

6 years ago

1.1.0

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago