Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gatsby-plugin-sitemap): include path prefix #12852

Merged
merged 4 commits into from
Mar 27, 2019
Merged
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
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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: don't need to change this (we do use it in different places as well), but for future, please use

expect(sitemapSpy).toBeCalledWith(
  expect.objectContaining({
    hostname: `${queryResult.data.site.siteMetadata.siteUrl}${prefix}`
  })
)

reaching to .mock.calls[0][0] works, but is less readable

`${queryResult.data.site.siteMetadata.siteUrl}${prefix}`
)
wardpeet marked this conversation as resolved.
Show resolved Hide resolved
})
})
})
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