diff --git a/packages/gatsby-plugin-sitemap/src/__tests__/internals.js b/packages/gatsby-plugin-sitemap/src/__tests__/internals.js index 53fc6deb2793d..f1a3dd7004062 100644 --- a/packages/gatsby-plugin-sitemap/src/__tests__/internals.js +++ b/packages/gatsby-plugin-sitemap/src/__tests__/internals.js @@ -69,6 +69,20 @@ describe(`results using default settings`, () => { verifyUrlsExistInResults(urls, [`http://dummy.url${pathPrefix}/page-1`]) }) + + it(`should fail when siteUrl is not set`, async () => { + const graphql = () => + Promise.resolve(generateQueryResultsMock({ siteUrl: null })) + expect.assertions(1) + + try { + await runQuery(graphql, ``, [], pathPrefix) + } catch (err) { + expect(err.message).toEqual( + expect.stringContaining(`SiteMetaData 'siteUrl' property is required`) + ) + } + }) } describe(`no path-prefix`, () => { diff --git a/packages/gatsby-plugin-sitemap/src/internals.js b/packages/gatsby-plugin-sitemap/src/internals.js index 3be0785849ef2..72b9b75ed336c 100644 --- a/packages/gatsby-plugin-sitemap/src/internals.js +++ b/packages/gatsby-plugin-sitemap/src/internals.js @@ -29,13 +29,17 @@ export const runQuery = (handler, query, excludes, pathPrefix) => return page }) - if (r.data.site.siteMetadata.siteUrl) { - // remove trailing slash of siteUrl - r.data.site.siteMetadata.siteUrl = withoutTrailingSlash( - r.data.site.siteMetadata.siteUrl + if (!r.data.site.siteMetadata.siteUrl) { + throw new Error( + `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` ) } + // remove trailing slash of siteUrl + r.data.site.siteMetadata.siteUrl = withoutTrailingSlash( + r.data.site.siteMetadata.siteUrl + ) + return r.data })