Skip to content

Commit 1d7e6c7

Browse files
bennetthardwickpieh
authored andcommitted
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.
1 parent 83ad448 commit 1d7e6c7

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

packages/gatsby-plugin-sitemap/src/__tests__/__snapshots__/gatsby-node.js.snap

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ exports[`Test plugin sitemap default settings work properly 1`] = `
1515
</urlset>"
1616
`;
1717
18-
exports[`Test plugin sitemap sitemap index set sitempa size and urls are less than it. 1`] = `
18+
exports[`Test plugin sitemap sitemap index set sitemap size and urls are less than it. 1`] = `
1919
"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?>
2020
<urlset xmlns=\\"http://www.sitemaps.org/schemas/sitemap/0.9\\" xmlns:news=\\"http://www.google.com/schemas/sitemap-news/0.9\\" xmlns:xhtml=\\"http://www.w3.org/1999/xhtml\\" xmlns:mobile=\\"http://www.google.com/schemas/sitemap-mobile/1.0\\" xmlns:image=\\"http://www.google.com/schemas/sitemap-image/1.1\\" xmlns:video=\\"http://www.google.com/schemas/sitemap-video/1.1\\">
2121
<url> <loc>http://dummy.url/page-1</loc> <changefreq>daily</changefreq> <priority>0.7</priority> </url>
2222
<url> <loc>http://dummy.url/page-2</loc> <changefreq>daily</changefreq> <priority>0.7</priority> </url>
2323
</urlset>"
2424
`;
25+

packages/gatsby-plugin-sitemap/src/__tests__/gatsby-node.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const path = require(`path`)
55
const { onPostBuild } = require(`../gatsby-node`)
66
const internals = require(`../internals`)
77
const pathPrefix = ``
8+
const sitemap = require(`sitemap`)
89

910
describe(`Test plugin sitemap`, async () => {
1011
it(`default settings work properly`, async () => {
@@ -167,7 +168,7 @@ describe(`Test plugin sitemap`, async () => {
167168
expect(originalFile).toEqual(path.join(`public`, `sitemap-index.xml`))
168169
expect(newFile).toEqual(path.join(`public`, `sitemap.xml`))
169170
})
170-
it(`set sitempa size and urls are less than it.`, async () => {
171+
it(`set sitemap size and urls are less than it.`, async () => {
171172
const options = {
172173
sitemapSize: 100,
173174
}
@@ -176,5 +177,16 @@ describe(`Test plugin sitemap`, async () => {
176177
expect(filePath).toEqual(path.join(`public`, `sitemap.xml`))
177178
expect(contents).toMatchSnapshot()
178179
})
180+
it(`should include path prefix when creating creating index sitemap`, async () => {
181+
const sitemapSpy = jest.spyOn(sitemap, `createSitemapIndex`)
182+
const options = {
183+
sitemapSize: 1,
184+
}
185+
const prefix = `/test`
186+
await onPostBuild({ graphql, pathPrefix: prefix }, options)
187+
expect(sitemapSpy.mock.calls[0][0].hostname).toEqual(
188+
`${queryResult.data.site.siteMetadata.siteUrl}${prefix}`
189+
)
190+
})
179191
})
180192
})

packages/gatsby-plugin-sitemap/src/gatsby-node.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ exports.onPostBuild = async ({ graphql, pathPrefix }, pluginOptions) => {
5252
)
5353
const sitemapIndexOptions = {
5454
...rest,
55-
hostname: hostname || withoutTrailingSlash(siteUrl),
55+
hostname:
56+
(hostname || withoutTrailingSlash(siteUrl)) +
57+
withoutTrailingSlash(pathPrefix || ``),
5658
targetFolder: publicPath,
5759
urls,
5860
callback: error => {

0 commit comments

Comments
 (0)