Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: several sitemap related issues #441

Merged
merged 3 commits into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
})
});