diff --git a/packages/gatsby-plugin-sitemap/src/__tests__/gatsby-node.js b/packages/gatsby-plugin-sitemap/src/__tests__/gatsby-node.js index 44f11c269e478..712cb8f4fcfce 100644 --- a/packages/gatsby-plugin-sitemap/src/__tests__/gatsby-node.js +++ b/packages/gatsby-plugin-sitemap/src/__tests__/gatsby-node.js @@ -19,6 +19,12 @@ const reporter = { panic: jest.fn(), } +const mockStoreGetState = jest.fn(() => ({config: {}})) + +const store = { + getState: mockStoreGetState +} + beforeEach(() => { global.__PATH_PREFIX__ = `` @@ -48,7 +54,7 @@ describe(`gatsby-plugin-sitemap Node API`, () => { }, }) await onPostBuild( - { graphql, pathPrefix, reporter }, + { graphql, pathPrefix, reporter, store }, await schema.validateAsync({}) ) const { @@ -106,7 +112,7 @@ describe(`gatsby-plugin-sitemap Node API`, () => { } await onPostBuild( - { graphql, pathPrefix, reporter }, + { graphql, pathPrefix, reporter, store }, await schema.validateAsync(options) ) @@ -147,7 +153,7 @@ describe(`gatsby-plugin-sitemap Node API`, () => { } const prefix = `/test` await onPostBuild( - { graphql, pathPrefix: prefix, reporter }, + { graphql, pathPrefix: prefix, reporter, store }, await schema.validateAsync(options) ) const { sourceData } = sitemap.simpleSitemapAndIndex.mock.calls[0][0] @@ -182,8 +188,15 @@ describe(`gatsby-plugin-sitemap Node API`, () => { const options = { output: subdir, } + + mockStoreGetState.mockImplementationOnce(() => ({ + config: { + assetPrefix: 'assets' + } + })) + await onPostBuild( - { graphql, pathPrefix: prefix, reporter }, + { graphql, pathPrefix: prefix, reporter, store }, await schema.validateAsync(options) ) expect(sitemap.simpleSitemapAndIndex.mock.calls[0][0].publicBasePath).toBe( diff --git a/packages/gatsby-plugin-sitemap/src/__tests__/internals.js b/packages/gatsby-plugin-sitemap/src/__tests__/internals.js index ad125320717a6..26f326a43d680 100644 --- a/packages/gatsby-plugin-sitemap/src/__tests__/internals.js +++ b/packages/gatsby-plugin-sitemap/src/__tests__/internals.js @@ -27,14 +27,31 @@ describe(`gatsby-plugin-sitemap internals tests`, () => { expect(result2).toStrictEqual(`/test/path`) }) - it(`prefixPath should correctly concatonate path`, () => { - const result = prefixPath({ + it(`prefixPath should correctly concatenate path`, () => { + const resultIfPathPrefix = prefixPath({ url: TestPath, siteUrl: SiteUrl, pathPrefix: `/root`, }) - expect(result).toStrictEqual(`https://example.net/root/test/path/`) + expect(resultIfPathPrefix).toStrictEqual(`https://example.net/root/test/path/`) + + const resultIfAssetPrefix = prefixPath({ + url: TestPath, + siteUrl: SiteUrl, + assetPrefix: 'assets' + }) + + expect(resultIfAssetPrefix).toStrictEqual(`https://example.net/test/path/`) + + const resultIfAssetAndPathPrefix = prefixPath({ + url: TestPath, + siteUrl: SiteUrl, + pathPrefix: `/assets/root`, + assetPrefix: 'assets' + }) + + expect(resultIfAssetAndPathPrefix).toStrictEqual(`https://example.net/root/test/path/`) }) it(`resolveSiteUrl should return SiteUrl`, () => { diff --git a/packages/gatsby-plugin-sitemap/src/gatsby-node.js b/packages/gatsby-plugin-sitemap/src/gatsby-node.js index 0021f33b17449..a0bca9ba2ab0f 100644 --- a/packages/gatsby-plugin-sitemap/src/gatsby-node.js +++ b/packages/gatsby-plugin-sitemap/src/gatsby-node.js @@ -6,7 +6,7 @@ import { prefixPath, pageFilter, REPORTER_PREFIX } from "./internals" exports.pluginOptionsSchema = pluginOptionsSchema exports.onPostBuild = async ( - { graphql, reporter, pathPrefix }, + { graphql, reporter, pathPrefix, store }, { output, entryLimit, @@ -66,6 +66,10 @@ exports.onPostBuild = async ( `${REPORTER_PREFIX} ${filteredPages.length} pages remain after filtering` ) + const { assetPrefix } = store.getState().config + + + const serializedPages = [] for (const page of filteredPages) { @@ -74,7 +78,7 @@ exports.onPostBuild = async ( serialize(page, { resolvePagePath }) ) serializedPages.push({ - url: prefixPath({ url, siteUrl, pathPrefix }), + url: prefixPath({ url, siteUrl, pathPrefix, assetPrefix }), ...rest, }) } catch (err) { diff --git a/packages/gatsby-plugin-sitemap/src/internals.js b/packages/gatsby-plugin-sitemap/src/internals.js index 255e25a8c303b..80e913e04ee63 100644 --- a/packages/gatsby-plugin-sitemap/src/internals.js +++ b/packages/gatsby-plugin-sitemap/src/internals.js @@ -19,9 +19,9 @@ export const withoutTrailingSlash = path => * @param {string} siteUrl - results of the resolveSiteUrl function * @returns {string} */ -// TODO: Update for v3 - Fix janky path/asset prefixing -export function prefixPath({ url, siteUrl, pathPrefix = `` }) { - return new URL(pathPrefix + url, siteUrl).toString() +export function prefixPath({ url, siteUrl, pathPrefix = ``, assetPrefix }) { + const purePathPrefix = !assetPrefix ? pathPrefix : pathPrefix.replace(`/${assetPrefix}`, '') + return new URL(purePathPrefix + url, siteUrl).toString() } /**