Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: configurable query for site URL #36

Merged
merged 3 commits into from
Jan 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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