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

(gatsby-plugin-sitemap) omit assetPrefix from page URLs #31454

Closed
wants to merge 3 commits into from
Closed
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
21 changes: 17 additions & 4 deletions packages/gatsby-plugin-sitemap/src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ const reporter = {
panic: jest.fn(),
}

const mockStoreGetState = jest.fn(() => ({config: {}}))

const store = {
getState: mockStoreGetState
}

beforeEach(() => {
global.__PATH_PREFIX__ = ``

Expand Down Expand Up @@ -48,7 +54,7 @@ describe(`gatsby-plugin-sitemap Node API`, () => {
},
})
await onPostBuild(
{ graphql, pathPrefix, reporter },
{ graphql, pathPrefix, reporter, store },
await schema.validateAsync({})
)
const {
Expand Down Expand Up @@ -106,7 +112,7 @@ describe(`gatsby-plugin-sitemap Node API`, () => {
}

await onPostBuild(
{ graphql, pathPrefix, reporter },
{ graphql, pathPrefix, reporter, store },
await schema.validateAsync(options)
)

Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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(
Expand Down
23 changes: 20 additions & 3 deletions packages/gatsby-plugin-sitemap/src/__tests__/internals.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`, () => {
Expand Down
8 changes: 6 additions & 2 deletions packages/gatsby-plugin-sitemap/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions packages/gatsby-plugin-sitemap/src/internals.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

/**
Expand Down