From 84ef4b5422557fac193134b5a7cd648579f04c7f Mon Sep 17 00:00:00 2001 From: Alex Moon Date: Sun, 2 May 2021 12:44:51 -0700 Subject: [PATCH] fix: correctly prefix sitemaps with pathPrefix --- .../src/__tests__/gatsby-node.js | 38 +++++++++++++++++++ .../gatsby-plugin-sitemap/src/gatsby-node.js | 3 +- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/packages/gatsby-plugin-sitemap/src/__tests__/gatsby-node.js b/packages/gatsby-plugin-sitemap/src/__tests__/gatsby-node.js index f2d68df47ffdf..d1138f26eb60c 100644 --- a/packages/gatsby-plugin-sitemap/src/__tests__/gatsby-node.js +++ b/packages/gatsby-plugin-sitemap/src/__tests__/gatsby-node.js @@ -155,4 +155,42 @@ describe(`gatsby-plugin-sitemap Node API`, () => { expect(page.url).toEqual(expect.stringContaining(prefix)) }) }) + + it(`should output modified paths to sitemap`, async () => { + const graphql = jest.fn() + graphql.mockResolvedValue({ + data: { + site: { + siteMetadata: { + siteUrl: `http://dummy.url`, + }, + }, + allSitePage: { + nodes: [ + { + path: `/page-1`, + }, + { + path: `/page-2`, + }, + ], + }, + }, + }) + const prefix = `/test` + const subdir = `/subdir` + const options = { + output: subdir, + } + await onPostBuild( + { graphql, pathPrefix: prefix, reporter }, + await schema.validateAsync(options) + ) + expect(sitemap.simpleSitemapAndIndex.mock.calls[0][0].publicBasePath).toBe( + path.posix.join(prefix, subdir) + ) + expect(sitemap.simpleSitemapAndIndex.mock.calls[0][0].destinationDir).toBe( + path.join(`public`, subdir) + ) + }) }) diff --git a/packages/gatsby-plugin-sitemap/src/gatsby-node.js b/packages/gatsby-plugin-sitemap/src/gatsby-node.js index 3aeb81674b150..b295ecca12bad 100644 --- a/packages/gatsby-plugin-sitemap/src/gatsby-node.js +++ b/packages/gatsby-plugin-sitemap/src/gatsby-node.js @@ -84,10 +84,11 @@ exports.onPostBuild = async ( } const sitemapWritePath = path.join(`public`, output) + const sitemapPublicPath = path.posix.join(pathPrefix, output) return simpleSitemapAndIndex({ hostname: siteUrl, - publicBasePath: output, + publicBasePath: sitemapPublicPath, destinationDir: sitemapWritePath, sourceData: serializedPages, limit: entryLimit,