From 1d7e6c7052fe09cfcb701b5b715b7babce773a21 Mon Sep 17 00:00:00 2001 From: Bennett Hardwick Date: Thu, 28 Mar 2019 04:47:57 +1000 Subject: [PATCH] fix(gatsby-plugin-sitemap): use pathPrefix when building sitemap index (#12852) ## Description The Gatsby sitemap plugin can be configured to split up all of the sites URLs into smaller sitemaps, all being referenced from a main "index" sitemap. Unfortunately, this index sitemap doesn't take the `pathPrefix` option into account and will never link to the correct "child" sitemaps. As `sitemap.js` doesn't have an option for `pathPrefix`, this change appends the `pathPrefix` to the site's `hostname` when more than one sitemap needs to be created. --- .../__tests__/__snapshots__/gatsby-node.js.snap | 3 ++- .../src/__tests__/gatsby-node.js | 14 +++++++++++++- packages/gatsby-plugin-sitemap/src/gatsby-node.js | 4 +++- 3 files changed, 18 insertions(+), 3 deletions(-) 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 => {