Skip to content

Commit

Permalink
feat: add env option (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdreizin committed Jul 7, 2018
1 parent a35cf4a commit 029e99d
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 22 deletions.
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ gatsby-*
!src/*
!test/*
!index.js

# jest
coverage/
78 changes: 70 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ or

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

## How to Use
## How To Use

`gatsby-config.js`

Expand All @@ -41,9 +41,7 @@ This plugin uses [`generate-robotstxt`](https://github.com/itgalaxy/generate-rob
| :-------: | :--------: | :-----------------------------------: | :--------------------: |
| `host` | `String` | `${siteMetadata.siteUrl}` | Host of your site |
| `sitemap` | `String` | `${siteMetadata.siteUrl}/sitemap.xml` | Path to `sitemap.xml` |
| `policy` | `Policy[]` | `[]` | List of `Policy` rules |

You can specify any allowed [`generate-robotstxt`](https://github.com/itgalaxy/generate-robotstxt#usage) options:
| `policy` | `Policy[]` | `[]` | List of [`Policy`](https://github.com/itgalaxy/generate-robotstxt#usage) rules |

`gatsby-config.js`

Expand All @@ -62,7 +60,60 @@ module.exports = {
};
```

### Netlify
### `env`-option

You can use the `env` option to set specific options in specific environment:

```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.NODE_ENV`, when this is not available then it defaults to `development`.

You can resolve the `env` key by using `resolveEnv` function:

```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: '/' }]
}
}
}
}
]
};
```

#### Netlify

If you would like to disable crawlers for [deploy-previews](https://www.netlify.com/blog/2016/07/20/introducing-deploy-previews-in-netlify/) you can use the following snippet:

Expand All @@ -85,13 +136,24 @@ module.exports = {
plugins: [
{
resolve: 'gatsby-plugin-robots-txt',
options: isNetlifyProduction
? { policy: [{ userAgent: '*' }] }
: {
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
}
}
}
}
]
};
Expand Down
24 changes: 19 additions & 5 deletions src/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'fs';
import url from 'url';
import path from 'path';
import robotsTxt from 'generate-robotstxt';
import path from 'path';
import url from 'url';

const publicPath = './public';
const query = `{
Expand All @@ -12,6 +12,7 @@ const query = `{
}
}
`;
const defaultEnv = 'development';

function writeFile(file, data) {
return new Promise((resolve, reject) => {
Expand All @@ -35,10 +36,23 @@ function runQuery(handler, query) {
});
}

export async function onPostBuild({ graphql }, pluginOptions) {
const userOptions = { ...pluginOptions };
const getOptions = pluginOptions => {
const options = { ...pluginOptions };

delete options.plugins;

delete userOptions.plugins;
const { env = {}, resolveEnv = () => process.env.NODE_ENV } = options;

const envOptions = env[resolveEnv()] || env[defaultEnv] || {};

delete options.env;
delete options.resolveEnv;

return { ...options, envOptions };
};

export async function onPostBuild({ graphql }, pluginOptions) {
const userOptions = getOptions(pluginOptions);

const defaultOptions = {
output: '/robots.txt'
Expand Down
20 changes: 18 additions & 2 deletions test/__snapshots__/gatsby-node.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`onPostBuild should generate \`robots.txt\` using \`env\` options 1`] = `
"User-agent: *
Allow: /
Sitemap: https://www.test.com/sitemap.xml
Host: https://www.test.com
"
`;

exports[`onPostBuild should generate \`robots.txt\` using \`env\` options and \`resolveEnv\` function 1`] = `
"User-agent: *
Allow: /
Sitemap: https://www.test.com/sitemap.xml
Host: https://www.test.com
"
`;

exports[`onPostBuild should generate \`robots.txt\` using \`graphql\` options 1`] = `
"User-agent: *
Allow: /
Expand All @@ -11,7 +27,7 @@ Host: https://www.test.com
exports[`onPostBuild should generate \`robots.txt\` using options 1`] = `
"User-agent: *
Allow: /
Sitemap: https://www.test1.com/sitemap.xml
Host: https://www.test1.com
Sitemap: https://www.test.com/sitemap.xml
Host: https://www.test.com
"
`;
59 changes: 52 additions & 7 deletions test/gatsby-node.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'path';
import MemoryFs from 'memory-fs';
import path from 'path';

const mockFs = jest.fn(() => new MemoryFs());

Expand Down Expand Up @@ -33,7 +33,7 @@ describe('onPostBuild', () => {
});

it('should generate `robots.txt` using options', async () => {
const output = './robots1.txt';
const output = './robots.txt';

await onPostBuild(
{
Expand All @@ -42,8 +42,8 @@ describe('onPostBuild', () => {
}
},
{
host: 'https://www.test1.com',
sitemap: 'https://www.test1.com/sitemap.xml',
host: 'https://www.test.com',
sitemap: 'https://www.test.com/sitemap.xml',
output
}
);
Expand All @@ -52,7 +52,7 @@ describe('onPostBuild', () => {
});

it('should generate `robots.txt` using `graphql` options', async () => {
const output = './robots2.txt';
const output = './robots-graphql.txt';

await onPostBuild(
{
Expand All @@ -69,7 +69,7 @@ describe('onPostBuild', () => {
});

it('should not generate `robots.txt` in case of `graphql` errors', async () => {
const output = './robots3.txt';
const output = './robots-graphql-err.txt';

await expect(
onPostBuild(
Expand All @@ -86,7 +86,7 @@ describe('onPostBuild', () => {
});

it('should not generate `robots.txt` in case of I/O errors', async () => {
const output = './robots4.txt';
const output = './robots-io-err.txt';

const spy = jest
.spyOn(fs, 'writeFile')
Expand All @@ -109,4 +109,49 @@ describe('onPostBuild', () => {

spy.mockRestore();
});

it('should generate `robots.txt` using `env` options', async () => {
const output = './robots-env.txt';

await onPostBuild(
{
graphql() {
return Promise.resolve({ data: {} });
}
},
{
host: 'https://www.test.com',
sitemap: 'https://www.test.com/sitemap.xml',
output,
env: {
test: {
policy: [{ userAgent: '*', disallow: ['/'] }]
}
}
}
);

expect(readContent(output)).toMatchSnapshot();
});

it('should generate `robots.txt` using `env` options and `resolveEnv` function', async () => {
const output = './robots-env-custom.txt';

await onPostBuild(
{
graphql() {
return Promise.resolve({ data: {} });
}
},
{
host: 'https://www.test.com',
sitemap: 'https://www.test.com/sitemap.xml',
output,
resolveEnv: () => 'custom',
env: { custom: { policy: [{ userAgent: '*', disallow: ['/'] }] } }
}
);

expect(readContent(output)).toMatchSnapshot();
});
});

0 comments on commit 029e99d

Please sign in to comment.