Skip to content

Commit

Permalink
fix: don't require siteUrl when host and sitemap are set (#13)
Browse files Browse the repository at this point in the history
* chore: format README.md

* fix: don't require siteUrl when host and sitemap are set

While the case of siteUrl not beeing present was handled, the code didn't
actually run because the graphql client emits an error when a non present field
is queried. This changes the behaviour so that a query is only executed when
either host or sitemap aren't set.

The change should be non-breaking.
  • Loading branch information
fahrradflucht authored and mdreizin committed Apr 29, 2018
1 parent 78a2814 commit d343a05
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,6 @@ module.exports = {
};
```


## 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)

[![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)
32 changes: 22 additions & 10 deletions src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,32 @@ function runQuery(handler, query) {
}

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

delete options.plugins;
delete userOptions.plugins;

const { site: { siteMetadata: { siteUrl } = {} } = {} } = await runQuery(
graphql,
query
);
const defaultOptions = {
output: '/robots.txt',
host: siteUrl,
sitemap: siteUrl && url.resolve(siteUrl, 'sitemap.xml')
output: '/robots.txt'
};
const { policy, sitemap, host, output } = { ...defaultOptions, ...options };

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

if (
!mergedOptions.hasOwnProperty('host') ||
!mergedOptions.hasOwnProperty('sitemap')
) {
const {
site: {
siteMetadata: { siteUrl }
}
} = await runQuery(graphql, query);

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

const { policy, sitemap, host, output } = mergedOptions;

const content = await robotsTxt({
policy,
sitemap,
Expand Down

0 comments on commit d343a05

Please sign in to comment.