Skip to content

Commit ae700a3

Browse files
lewismorrisLewis Morris
and
Lewis Morris
authored
Fixes #451 Add null checks to both host and sitemap properties. Update tests. (#482)
Co-authored-by: Lewis Morris <[email protected]>
1 parent 313746f commit ae700a3

File tree

3 files changed

+74
-21
lines changed

3 files changed

+74
-21
lines changed

src/gatsby-node.js

+24-21
Original file line numberDiff line numberDiff line change
@@ -56,32 +56,35 @@ export async function onPostBuild({ graphql, pathPrefix = "" }, pluginOptions) {
5656
const userOptions = getOptions(pluginOptions);
5757
const mergedOptions = { ...defaultOptions, ...userOptions };
5858

59-
if (
60-
!Object.prototype.hasOwnProperty.call(mergedOptions, 'host')
61-
) {
62-
const {
63-
site: {
64-
siteMetadata: { siteUrl }
65-
}
66-
} = await runQuery(graphql, mergedOptions.query);
67-
68-
mergedOptions.host = siteUrl;
59+
if(mergedOptions.host !== null) {
60+
if (
61+
!Object.prototype.hasOwnProperty.call(mergedOptions, 'host')
62+
) {
63+
const {
64+
site: {
65+
siteMetadata: { siteUrl }
66+
}
67+
} = await runQuery(graphql, mergedOptions.query);
68+
69+
mergedOptions.host = siteUrl;
70+
}
6971
}
7072

71-
if (
72-
!Object.prototype.hasOwnProperty.call(mergedOptions, 'sitemap')
73-
) {
74-
75-
mergedOptions.sitemap = new URL(path.posix.join(pathPrefix, 'sitemap', 'sitemap-index.xml'), mergedOptions.host).toString();
76-
} else {
77-
try {
78-
new URL(mergedOptions.sitemap)
79-
} catch {
80-
mergedOptions.sitemap = new URL(mergedOptions.sitemap.startsWith(pathPrefix) ? mergedOptions.sitemap : path.posix.join(pathPrefix, mergedOptions.sitemap), mergedOptions.host).toString()
73+
if(mergedOptions.sitemap !== null) {
74+
if (
75+
!Object.prototype.hasOwnProperty.call(mergedOptions, 'sitemap')
76+
) {
77+
78+
mergedOptions.sitemap = new URL(path.posix.join(pathPrefix, 'sitemap', 'sitemap-index.xml'), mergedOptions.host).toString();
79+
} else {
80+
try {
81+
new URL(mergedOptions.sitemap)
82+
} catch {
83+
mergedOptions.sitemap = new URL(mergedOptions.sitemap.startsWith(pathPrefix) ? mergedOptions.sitemap : path.posix.join(pathPrefix, mergedOptions.sitemap), mergedOptions.host).toString()
84+
}
8185
}
8286
}
8387

84-
8588
const { policy, sitemap, host, output, configFile } = mergedOptions;
8689

8790
const content = await robotsTxt({

test/__snapshots__/gatsby-node.test.js.snap

+14
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,17 @@ Sitemap: https://www.test.com/sitemap.xml
3131
Host: https://www.test.com
3232
"
3333
`;
34+
35+
exports[`onPostBuild should generate a \`robots.txt\` without a host property 1`] = `
36+
"User-agent: *
37+
Allow: /
38+
Sitemap: https://www.test.com/sitemap.xml
39+
"
40+
`;
41+
42+
exports[`onPostBuild should generate a \`robots.txt\` without a sitemap property 1`] = `
43+
"User-agent: *
44+
Allow: /
45+
Host: https://www.test.com
46+
"
47+
`;

test/gatsby-node.test.js

+36
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,42 @@ describe('onPostBuild', () => {
6262
expect(readContent(output)).toMatchSnapshot();
6363
});
6464

65+
it('should generate a `robots.txt` without a host property', async () => {
66+
const output = './robots-host-null.txt';
67+
68+
await onPostBuild(
69+
{
70+
graphql() {
71+
return Promise.resolve({ data: {} })
72+
}
73+
},
74+
{
75+
host: null,
76+
sitemap: 'https://www.test.com/sitemap.xml',
77+
output
78+
})
79+
80+
expect(readContent(output)).toMatchSnapshot();
81+
})
82+
83+
it('should generate a `robots.txt` without a sitemap property', async () => {
84+
const output = './robots-sitemap-null.txt';
85+
86+
await onPostBuild(
87+
{
88+
graphql() {
89+
return Promise.resolve({ data: {} })
90+
}
91+
},
92+
{
93+
host: 'https://www.test.com',
94+
sitemap: null,
95+
output
96+
})
97+
98+
expect(readContent(output)).toMatchSnapshot();
99+
})
100+
65101
it('should not generate `robots.txt` in case of `graphql` errors', async () => {
66102
const output = './robots-graphql-err.txt';
67103

0 commit comments

Comments
 (0)