diff --git a/packages/gatsby-plugin-sitemap/src/__tests__/__snapshots__/gatsby-node.js.snap b/packages/gatsby-plugin-sitemap/src/__tests__/__snapshots__/gatsby-node.js.snap index d628cf8c527b7..96ee1126fbfba 100644 --- a/packages/gatsby-plugin-sitemap/src/__tests__/__snapshots__/gatsby-node.js.snap +++ b/packages/gatsby-plugin-sitemap/src/__tests__/__snapshots__/gatsby-node.js.snap @@ -15,10 +15,11 @@ exports[`Test plugin sitemap default settings work properly 1`] = ` " `; -exports[`Test plugin sitemap sitemap index set sitempa size and urls are less than it. 1`] = ` +exports[`Test plugin sitemap sitemap index set sitemap size and urls are less than it. 1`] = ` " http://dummy.url/page-1 daily 0.7 http://dummy.url/page-2 daily 0.7 " `; + diff --git a/packages/gatsby-plugin-sitemap/src/__tests__/gatsby-node.js b/packages/gatsby-plugin-sitemap/src/__tests__/gatsby-node.js index 690f1888cf837..4afe8566cfd9b 100644 --- a/packages/gatsby-plugin-sitemap/src/__tests__/gatsby-node.js +++ b/packages/gatsby-plugin-sitemap/src/__tests__/gatsby-node.js @@ -5,6 +5,7 @@ const path = require(`path`) const { onPostBuild } = require(`../gatsby-node`) const internals = require(`../internals`) const pathPrefix = `` +const sitemap = require(`sitemap`) describe(`Test plugin sitemap`, async () => { it(`default settings work properly`, async () => { @@ -167,7 +168,7 @@ describe(`Test plugin sitemap`, async () => { expect(originalFile).toEqual(path.join(`public`, `sitemap-index.xml`)) expect(newFile).toEqual(path.join(`public`, `sitemap.xml`)) }) - it(`set sitempa size and urls are less than it.`, async () => { + it(`set sitemap size and urls are less than it.`, async () => { const options = { sitemapSize: 100, } @@ -176,5 +177,16 @@ describe(`Test plugin sitemap`, async () => { expect(filePath).toEqual(path.join(`public`, `sitemap.xml`)) expect(contents).toMatchSnapshot() }) + it(`should include path prefix when creating creating index sitemap`, async () => { + const sitemapSpy = jest.spyOn(sitemap, `createSitemapIndex`) + const options = { + sitemapSize: 1, + } + const prefix = `/test` + await onPostBuild({ graphql, pathPrefix: prefix }, options) + expect(sitemapSpy.mock.calls[0][0].hostname).toEqual( + `${queryResult.data.site.siteMetadata.siteUrl}${prefix}` + ) + }) }) }) diff --git a/packages/gatsby-plugin-sitemap/src/gatsby-node.js b/packages/gatsby-plugin-sitemap/src/gatsby-node.js index 1ab7344c1a603..9cdc218bb4a8d 100644 --- a/packages/gatsby-plugin-sitemap/src/gatsby-node.js +++ b/packages/gatsby-plugin-sitemap/src/gatsby-node.js @@ -52,7 +52,9 @@ exports.onPostBuild = async ({ graphql, pathPrefix }, pluginOptions) => { ) const sitemapIndexOptions = { ...rest, - hostname: hostname || withoutTrailingSlash(siteUrl), + hostname: + (hostname || withoutTrailingSlash(siteUrl)) + + withoutTrailingSlash(pathPrefix || ``), targetFolder: publicPath, urls, callback: error => {