Skip to content

Commit

Permalink
fix(gatsby-plugin-sitemap): use pathPrefix when building sitemap index (
Browse files Browse the repository at this point in the history
#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.
  • Loading branch information
bennetthardwick authored and pieh committed Mar 27, 2019
1 parent 83ad448 commit 1d7e6c7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ exports[`Test plugin sitemap default settings work properly 1`] = `
</urlset>"
`;
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`] = `
"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?>
<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\\">
<url> <loc>http://dummy.url/page-1</loc> <changefreq>daily</changefreq> <priority>0.7</priority> </url>
<url> <loc>http://dummy.url/page-2</loc> <changefreq>daily</changefreq> <priority>0.7</priority> </url>
</urlset>"
`;
14 changes: 13 additions & 1 deletion packages/gatsby-plugin-sitemap/src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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,
}
Expand All @@ -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}`
)
})
})
})
4 changes: 3 additions & 1 deletion packages/gatsby-plugin-sitemap/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down

0 comments on commit 1d7e6c7

Please sign in to comment.