Skip to content

Commit

Permalink
feat: several sitemap related issues (#441)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
moonmeister committed May 10, 2021
1 parent 5dbfe2d commit e04abbc
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 6 deletions.
21 changes: 16 additions & 5 deletions src/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import fs from 'fs';
import robotsTxt from 'generate-robotstxt';
import path from 'path';
import url from 'url';

const publicPath = './public';
const defaultEnv = 'development';
Expand Down Expand Up @@ -53,13 +52,12 @@ const getOptions = pluginOptions => {
return { ...options, ...envOptions };
};

export async function onPostBuild({ graphql }, pluginOptions) {
export async function onPostBuild({ graphql, pathPrefix = "" }, pluginOptions) {
const userOptions = getOptions(pluginOptions);
const mergedOptions = { ...defaultOptions, ...userOptions };

if (
!Object.prototype.hasOwnProperty.call(mergedOptions, 'host') ||
!Object.prototype.hasOwnProperty.call(mergedOptions,'sitemap')
!Object.prototype.hasOwnProperty.call(mergedOptions, 'host')
) {
const {
site: {
Expand All @@ -68,9 +66,22 @@ export async function onPostBuild({ graphql }, pluginOptions) {
} = await runQuery(graphql, mergedOptions.query);

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

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
2 changes: 1 addition & 1 deletion test/__snapshots__/gatsby-node.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Host: https://www.test.com
exports[`onPostBuild should generate \`robots.txt\` using \`graphql\` options 1`] = `
"User-agent: *
Allow: /
Sitemap: https://www.test.com/sitemap.xml
Sitemap: https://www.test.com/sitemap/sitemap-index.xml
Host: https://www.test.com
"
`;
Expand Down
99 changes: 99 additions & 0 deletions test/gatsby-node.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,103 @@ describe('onPostBuild', () => {

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

it(`should set sitemap separate from host`, async () => {
const output = './robots-sitemap.txt';

await onPostBuild(
{
graphql() {
return Promise.resolve({ data: graphqlOptions });
}
},
{
sitemap: 'https://www.test.com/sitemap-test.xml',
output,
resolveEnv: () => 'custom',
env: { custom: { policy: [{ userAgent: '*', disallow: ['/'] }] } }
}
);

expect(readContent(output)).toContain('Sitemap: https://www.test.com/sitemap-test.xml');
})

it(`should set sitemap using host if not absolute`, async () => {
const output = './robots-sitemap-relative.txt';

await onPostBuild(
{
graphql() {
return Promise.resolve({ data: graphqlOptions });
}
},
{
sitemap: 'sitemap-test-relative.xml',
output,
resolveEnv: () => 'custom',
env: { custom: { policy: [{ userAgent: '*', disallow: ['/'] }] } }
}
);

expect(readContent(output)).toContain('Sitemap: https://www.test.com/sitemap-test-relative.xml');
})

it(`should add pathPrefix to defaults`, async () => {
const output = './robots-sitemap-prefix.txt';
const pathPrefix = '/prefix'

await onPostBuild(
{
graphql() {
return Promise.resolve({ data: graphqlOptions });
},
pathPrefix
},
{
output,
}
);

expect(readContent(output)).toContain('Sitemap: https://www.test.com/prefix/sitemap/sitemap-index.xml');
})

it(`should add pathPrefix to provided sitemap`, async () => {
const output = './robots-sitemap-prefix-provided.txt';
const pathPrefix = '/prefix'

await onPostBuild(
{
graphql() {
return Promise.resolve({ data: graphqlOptions });
},
pathPrefix
},
{
output,
sitemap: 'sitemap.xml'
}
);

expect(readContent(output)).toContain('Sitemap: https://www.test.com/prefix/sitemap.xml');
})

it(`should not add pathPrefix if provided sitemap alread has prefix`, async () => {
const output = './robots-sitemap-prefix-provided.txt';
const pathPrefix = '/prefix'

await onPostBuild(
{
graphql() {
return Promise.resolve({ data: graphqlOptions });
},
pathPrefix
},
{
output,
sitemap: '/prefix/sitemap.xml'
}
);

expect(readContent(output)).toContain('Sitemap: https://www.test.com/prefix/sitemap.xml');
})
});

0 comments on commit e04abbc

Please sign in to comment.