Skip to content

Commit e04abbc

Browse files
authored
feat: several sitemap related issues (#441)
* fix: make default sitemap match gatsby-plugin-sitemap, allow sitemap name to be set seperate from host, allow sitemap to be set using relative url * feat: add pathPrefix handling * fix: correct default path
1 parent 5dbfe2d commit e04abbc

File tree

3 files changed

+116
-6
lines changed

3 files changed

+116
-6
lines changed

src/gatsby-node.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import fs from 'fs';
22
import robotsTxt from 'generate-robotstxt';
33
import path from 'path';
4-
import url from 'url';
54

65
const publicPath = './public';
76
const defaultEnv = 'development';
@@ -53,13 +52,12 @@ const getOptions = pluginOptions => {
5352
return { ...options, ...envOptions };
5453
};
5554

56-
export async function onPostBuild({ graphql }, pluginOptions) {
55+
export async function onPostBuild({ graphql, pathPrefix = "" }, pluginOptions) {
5756
const userOptions = getOptions(pluginOptions);
5857
const mergedOptions = { ...defaultOptions, ...userOptions };
5958

6059
if (
61-
!Object.prototype.hasOwnProperty.call(mergedOptions, 'host') ||
62-
!Object.prototype.hasOwnProperty.call(mergedOptions,'sitemap')
60+
!Object.prototype.hasOwnProperty.call(mergedOptions, 'host')
6361
) {
6462
const {
6563
site: {
@@ -68,9 +66,22 @@ export async function onPostBuild({ graphql }, pluginOptions) {
6866
} = await runQuery(graphql, mergedOptions.query);
6967

7068
mergedOptions.host = siteUrl;
71-
mergedOptions.sitemap = url.resolve(siteUrl, 'sitemap.xml');
7269
}
7370

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()
81+
}
82+
}
83+
84+
7485
const { policy, sitemap, host, output, configFile } = mergedOptions;
7586

7687
const content = await robotsTxt({

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Host: https://www.test.com
1919
exports[`onPostBuild should generate \`robots.txt\` using \`graphql\` options 1`] = `
2020
"User-agent: *
2121
Allow: /
22-
Sitemap: https://www.test.com/sitemap.xml
22+
Sitemap: https://www.test.com/sitemap/sitemap-index.xml
2323
Host: https://www.test.com
2424
"
2525
`;

test/gatsby-node.test.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,103 @@ describe('onPostBuild', () => {
148148

149149
expect(readContent(output)).toMatchSnapshot();
150150
});
151+
152+
it(`should set sitemap separate from host`, async () => {
153+
const output = './robots-sitemap.txt';
154+
155+
await onPostBuild(
156+
{
157+
graphql() {
158+
return Promise.resolve({ data: graphqlOptions });
159+
}
160+
},
161+
{
162+
sitemap: 'https://www.test.com/sitemap-test.xml',
163+
output,
164+
resolveEnv: () => 'custom',
165+
env: { custom: { policy: [{ userAgent: '*', disallow: ['/'] }] } }
166+
}
167+
);
168+
169+
expect(readContent(output)).toContain('Sitemap: https://www.test.com/sitemap-test.xml');
170+
})
171+
172+
it(`should set sitemap using host if not absolute`, async () => {
173+
const output = './robots-sitemap-relative.txt';
174+
175+
await onPostBuild(
176+
{
177+
graphql() {
178+
return Promise.resolve({ data: graphqlOptions });
179+
}
180+
},
181+
{
182+
sitemap: 'sitemap-test-relative.xml',
183+
output,
184+
resolveEnv: () => 'custom',
185+
env: { custom: { policy: [{ userAgent: '*', disallow: ['/'] }] } }
186+
}
187+
);
188+
189+
expect(readContent(output)).toContain('Sitemap: https://www.test.com/sitemap-test-relative.xml');
190+
})
191+
192+
it(`should add pathPrefix to defaults`, async () => {
193+
const output = './robots-sitemap-prefix.txt';
194+
const pathPrefix = '/prefix'
195+
196+
await onPostBuild(
197+
{
198+
graphql() {
199+
return Promise.resolve({ data: graphqlOptions });
200+
},
201+
pathPrefix
202+
},
203+
{
204+
output,
205+
}
206+
);
207+
208+
expect(readContent(output)).toContain('Sitemap: https://www.test.com/prefix/sitemap/sitemap-index.xml');
209+
})
210+
211+
it(`should add pathPrefix to provided sitemap`, async () => {
212+
const output = './robots-sitemap-prefix-provided.txt';
213+
const pathPrefix = '/prefix'
214+
215+
await onPostBuild(
216+
{
217+
graphql() {
218+
return Promise.resolve({ data: graphqlOptions });
219+
},
220+
pathPrefix
221+
},
222+
{
223+
output,
224+
sitemap: 'sitemap.xml'
225+
}
226+
);
227+
228+
expect(readContent(output)).toContain('Sitemap: https://www.test.com/prefix/sitemap.xml');
229+
})
230+
231+
it(`should not add pathPrefix if provided sitemap alread has prefix`, async () => {
232+
const output = './robots-sitemap-prefix-provided.txt';
233+
const pathPrefix = '/prefix'
234+
235+
await onPostBuild(
236+
{
237+
graphql() {
238+
return Promise.resolve({ data: graphqlOptions });
239+
},
240+
pathPrefix
241+
},
242+
{
243+
output,
244+
sitemap: '/prefix/sitemap.xml'
245+
}
246+
);
247+
248+
expect(readContent(output)).toContain('Sitemap: https://www.test.com/prefix/sitemap.xml');
249+
})
151250
});

0 commit comments

Comments
 (0)