Skip to content
Open
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
58 changes: 54 additions & 4 deletions src/__tests__/gatsby-node.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const path = require(`path`);
const { onPostBuild } = require(`../gatsby-node`);
const utils = require(`../utils`);

const basePath = undefined;
const pathPrefix = ``;

beforeEach(() => {
Expand Down Expand Up @@ -53,7 +54,7 @@ describe(`Test plugin sitemap`, () => {
},
});

await onPostBuild({ graphql, pathPrefix }, {});
await onPostBuild({ graphql, pathPrefix, basePath }, {});

const [filePath] = utils.outputFile.mock.calls[0];

Expand Down Expand Up @@ -114,7 +115,7 @@ describe(`Test plugin sitemap`, () => {
slug: path
}
}
}
}
}`;

const options = {
Expand All @@ -128,7 +129,7 @@ describe(`Test plugin sitemap`, () => {
query: customQuery,
};

await onPostBuild({ graphql, pathPrefix }, options);
await onPostBuild({ graphql, pathPrefix, basePath }, options);

const [filePath] = utils.outputFile.mock.calls[0];

Expand Down Expand Up @@ -196,9 +197,58 @@ describe(`sitemap index`, () => {
utils.outputFile = jest.fn();
utils.outputFile.mockResolvedValue(true);

await onPostBuild({ graphql, pathPrefix }, options);
await onPostBuild({ graphql, pathPrefix, basePath }, options);
const [sitemap] = utils.outputFile.mock.calls[0];

expect(sitemap).toEqual(path.join(`public`, `sitemap.xml`));
});

it(`uses basePath instead of pathPrefix`, async () => {
const basePath = '/blog'
const pathPrefix = 'https://cdn.example.com/blog'

utils.writeFile = jest.fn();
utils.writeFile.mockResolvedValue(true);

utils.outputFile = jest.fn();
utils.outputFile.mockResolvedValue(true);

utils.readFile = jest.fn();
utils.readFile.mockResolvedValue(true);

const graphql = jest.fn();

graphql.mockResolvedValue({
data: {
site: {
siteMetadata: {
siteUrl: `http://dummy.url`,
},
},
allSitePage: {
edges: [
{
node: {
id: 1,
slug: `page-1`,
url: `http://dummy.url/page-1`,
},
},
{
node: {
id: 2,
slug: `page-2`,
url: `http://dummy.url/page-2`,
},
},
],
},
},
});

await onPostBuild({ graphql, pathPrefix, basePath }, {});
const [sitemap] = utils.outputFile.mock.calls[0];

expect(sitemap).toEqual(path.join(`public`, `blog`, `sitemap.xml`));
})
});
6 changes: 4 additions & 2 deletions src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ import { getNodePath } from './helpers';

let siteURL;

const copyStylesheet = async ({ siteUrl, pathPrefix, indexOutput }) => {
const copyStylesheet = async ({ siteUrl, basePath, indexOutput }) => {
const siteRegex = /(\{\{blog-url\}\})/g;

// Get our stylesheet template
const data = await utils.readFile(XSLFILE);

// Replace the `{{blog-url}}` variable with our real site URL
const pathPrefix = basePath || ``;
const sitemapStylesheet = data.toString().replace(siteRegex, new URL(path.join(pathPrefix, indexOutput), siteUrl).toString());

// Save the updated stylesheet to the public folder, so it will be
Expand Down Expand Up @@ -149,13 +150,14 @@ const serialize = ({ ...sources } = {}, { site, allSitePage }, { mapping, addUnc
return nodes;
};

exports.onPostBuild = async ({ graphql, pathPrefix }, pluginOptions) => {
exports.onPostBuild = async ({ graphql, basePath }, pluginOptions) => {
let queryRecords;

// Passing the config option addUncaughtPages will add all pages which are not covered by passed mappings
// to the default `pages` sitemap. Otherwise they will be ignored.
const options = pluginOptions.addUncaughtPages ? merge(defaultOptions, pluginOptions) : Object.assign({}, defaultOptions, pluginOptions);

const pathPrefix = basePath || ``;
const indexSitemapFile = path.join(PUBLICPATH, pathPrefix, options.output);
const resourcesSitemapFile = path.join(PUBLICPATH, pathPrefix, RESOURCESFILE);

Expand Down