Skip to content

Commit 65693d3

Browse files
sbardianwardpeet
authored andcommitted
fix(gatsby-plugin-sitemap): add meaningful error when siteUrl is missing (#13123)
## Description Throw a more descriptive error when users do not supply a `siteUrl` property when using `gatsby-plugin-sitemap` ## Related Issues Fixes #12912
1 parent 169c964 commit 65693d3

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

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

+14
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,20 @@ describe(`results using default settings`, () => {
6969

7070
verifyUrlsExistInResults(urls, [`http://dummy.url${pathPrefix}/page-1`])
7171
})
72+
73+
it(`should fail when siteUrl is not set`, async () => {
74+
const graphql = () =>
75+
Promise.resolve(generateQueryResultsMock({ siteUrl: null }))
76+
expect.assertions(1)
77+
78+
try {
79+
await runQuery(graphql, ``, [], pathPrefix)
80+
} catch (err) {
81+
expect(err.message).toEqual(
82+
expect.stringContaining(`SiteMetaData 'siteUrl' property is required`)
83+
)
84+
}
85+
})
7286
}
7387

7488
describe(`no path-prefix`, () => {

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

+8-4
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,17 @@ 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
32+
if (!r.data.site.siteMetadata.siteUrl) {
33+
throw new Error(
34+
`SiteMetaData 'siteUrl' property is required. Check out the documentation to see a working example: https://www.gatsbyjs.org/packages/gatsby-plugin-sitemap/#how-to-use`
3635
)
3736
}
3837

38+
// remove trailing slash of siteUrl
39+
r.data.site.siteMetadata.siteUrl = withoutTrailingSlash(
40+
r.data.site.siteMetadata.siteUrl
41+
)
42+
3943
return r.data
4044
})
4145

0 commit comments

Comments
 (0)