Skip to content

Commit 41bd265

Browse files
rnittapieh
authored andcommitted
feat(gatsby-plugin-sitemap): sanitize siteUrl (#12613)
## Description For sitemap plugin Add code to remove trailing slash of siteUrl. ## Related Issues Fix #12590
1 parent 2a50218 commit 41bd265

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
const {
2+
runQuery,
3+
defaultOptions: { serialize },
4+
} = require(`../internals`)
5+
6+
describe(`results using default settings`, () => {
7+
const generateQueryResultsMock = (
8+
{ siteUrl } = { siteUrl: `http://dummy.url` }
9+
) => {
10+
return {
11+
data: {
12+
site: {
13+
siteMetadata: {
14+
siteUrl: siteUrl,
15+
},
16+
},
17+
allSitePage: {
18+
edges: [
19+
{
20+
node: {
21+
path: `/page-1`,
22+
},
23+
},
24+
{
25+
node: {
26+
path: `/page-2`,
27+
},
28+
},
29+
],
30+
},
31+
},
32+
}
33+
}
34+
35+
const verifyUrlsExistInResults = (results, urls) => {
36+
expect(results.map(result => result.url)).toEqual(urls)
37+
}
38+
39+
const runTests = (pathPrefix = ``) => {
40+
it(`prepares all urls correctly`, async () => {
41+
const graphql = () => Promise.resolve(generateQueryResultsMock())
42+
const queryRecords = await runQuery(graphql, ``, [], pathPrefix)
43+
const urls = serialize(queryRecords)
44+
45+
verifyUrlsExistInResults(urls, [
46+
`http://dummy.url${pathPrefix}/page-1`,
47+
`http://dummy.url${pathPrefix}/page-2`,
48+
])
49+
})
50+
51+
it(`sanitize siteUrl`, async () => {
52+
const graphql = () =>
53+
Promise.resolve(
54+
generateQueryResultsMock({ siteUrl: `http://dummy.url/` })
55+
)
56+
const queryRecords = await runQuery(graphql, ``, [], pathPrefix)
57+
const urls = serialize(queryRecords)
58+
59+
verifyUrlsExistInResults(urls, [
60+
`http://dummy.url${pathPrefix}/page-1`,
61+
`http://dummy.url${pathPrefix}/page-2`,
62+
])
63+
})
64+
65+
it(`excludes pages`, async () => {
66+
const graphql = () => Promise.resolve(generateQueryResultsMock())
67+
const queryRecords = await runQuery(graphql, ``, [`/page-2`], pathPrefix)
68+
const urls = serialize(queryRecords)
69+
70+
verifyUrlsExistInResults(urls, [`http://dummy.url${pathPrefix}/page-1`])
71+
})
72+
}
73+
74+
describe(`no path-prefix`, () => {
75+
runTests()
76+
})
77+
78+
describe(`with path-prefix`, () => {
79+
runTests(`/path-prefix`)
80+
})
81+
})

packages/gatsby-plugin-sitemap/src/internals.js

+7
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ export const runQuery = (handler, query, excludes, pathPrefix) =>
2929
return page
3030
})
3131

32+
if (r.data.site.siteMetadata.siteUrl) {
33+
// remove trailing slash of siteUrl
34+
r.data.site.siteMetadata.siteUrl = withoutTrailingSlash(
35+
r.data.site.siteMetadata.siteUrl
36+
)
37+
}
38+
3239
return r.data
3340
})
3441

0 commit comments

Comments
 (0)