Skip to content

Commit

Permalink
Fixes #451 Add null checks to both host and sitemap properties. Updat…
Browse files Browse the repository at this point in the history
…e tests. (#482)

Co-authored-by: Lewis Morris <lewis@kafe.rocks>
  • Loading branch information
lewismorris and Lewis Morris committed Jul 27, 2021
1 parent 313746f commit ae700a3
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 21 deletions.
45 changes: 24 additions & 21 deletions src/gatsby-node.js
Expand Up @@ -56,32 +56,35 @@ export async function onPostBuild({ graphql, pathPrefix = "" }, pluginOptions) {
const userOptions = getOptions(pluginOptions);
const mergedOptions = { ...defaultOptions, ...userOptions };

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

mergedOptions.host = siteUrl;
if(mergedOptions.host !== null) {
if (
!Object.prototype.hasOwnProperty.call(mergedOptions, 'host')
) {
const {
site: {
siteMetadata: { siteUrl }
}
} = await runQuery(graphql, mergedOptions.query);

mergedOptions.host = siteUrl;
}
}

if (
!Object.prototype.hasOwnProperty.call(mergedOptions, 'sitemap')
) {

mergedOptions.sitemap = new URL(path.posix.join(pathPrefix, 'sitemap', 'sitemap-index.xml'), mergedOptions.host).toString();
} else {
try {
new URL(mergedOptions.sitemap)
} catch {
mergedOptions.sitemap = new URL(mergedOptions.sitemap.startsWith(pathPrefix) ? mergedOptions.sitemap : path.posix.join(pathPrefix, mergedOptions.sitemap), mergedOptions.host).toString()
if(mergedOptions.sitemap !== null) {
if (
!Object.prototype.hasOwnProperty.call(mergedOptions, 'sitemap')
) {

mergedOptions.sitemap = new URL(path.posix.join(pathPrefix, 'sitemap', 'sitemap-index.xml'), mergedOptions.host).toString();
} else {
try {
new URL(mergedOptions.sitemap)
} catch {
mergedOptions.sitemap = new URL(mergedOptions.sitemap.startsWith(pathPrefix) ? mergedOptions.sitemap : path.posix.join(pathPrefix, mergedOptions.sitemap), mergedOptions.host).toString()
}
}
}


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

const content = await robotsTxt({
Expand Down
14 changes: 14 additions & 0 deletions test/__snapshots__/gatsby-node.test.js.snap
Expand Up @@ -31,3 +31,17 @@ Sitemap: https://www.test.com/sitemap.xml
Host: https://www.test.com
"
`;

exports[`onPostBuild should generate a \`robots.txt\` without a host property 1`] = `
"User-agent: *
Allow: /
Sitemap: https://www.test.com/sitemap.xml
"
`;

exports[`onPostBuild should generate a \`robots.txt\` without a sitemap property 1`] = `
"User-agent: *
Allow: /
Host: https://www.test.com
"
`;
36 changes: 36 additions & 0 deletions test/gatsby-node.test.js
Expand Up @@ -62,6 +62,42 @@ describe('onPostBuild', () => {
expect(readContent(output)).toMatchSnapshot();
});

it('should generate a `robots.txt` without a host property', async () => {
const output = './robots-host-null.txt';

await onPostBuild(
{
graphql() {
return Promise.resolve({ data: {} })
}
},
{
host: null,
sitemap: 'https://www.test.com/sitemap.xml',
output
})

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

it('should generate a `robots.txt` without a sitemap property', async () => {
const output = './robots-sitemap-null.txt';

await onPostBuild(
{
graphql() {
return Promise.resolve({ data: {} })
}
},
{
host: 'https://www.test.com',
sitemap: null,
output
})

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

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

Expand Down

0 comments on commit ae700a3

Please sign in to comment.