Skip to content

Commit

Permalink
feat: configurable query for site URL (#36)
Browse files Browse the repository at this point in the history
* feat: configurable query for site URL
* docs: fix lint issue
  • Loading branch information
amclin authored and mdreizin committed Jan 26, 2019
1 parent e3c5be1 commit 2da7db0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,30 @@ module.exports = {
};
```

### Query
By default the site URL will come from the Gatsby node `site.siteMeta.siteUrl`. Like in [Gatsby's sitemap plugin](https://www.gatsbyjs.org/packages/gatsby-plugin-sitemap/) 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`

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

## License

[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmdreizin%2Fgatsby-plugin-robots-txt.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fmdreizin%2Fgatsby-plugin-robots-txt?ref=badge_large)
25 changes: 11 additions & 14 deletions src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ import path from 'path';
import url from 'url';

const publicPath = './public';
const query = `{
site {
siteMetadata {
siteUrl
}
}
}
`;
const defaultEnv = 'development';
const defaultOptions = {
output: '/robots.txt',
query: `{
site {
siteMetadata {
siteUrl
}
}
}`
};

function writeFile(file, data) {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -53,11 +55,6 @@ const getOptions = pluginOptions => {

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

const defaultOptions = {
output: '/robots.txt'
};

const mergedOptions = { ...defaultOptions, ...userOptions };

if (
Expand All @@ -68,7 +65,7 @@ export async function onPostBuild({ graphql }, pluginOptions) {
site: {
siteMetadata: { siteUrl }
}
} = await runQuery(graphql, query);
} = await runQuery(graphql, mergedOptions.query);

mergedOptions.host = siteUrl;
mergedOptions.sitemap = url.resolve(siteUrl, 'sitemap.xml');
Expand Down

0 comments on commit 2da7db0

Please sign in to comment.