Skip to content

Commit

Permalink
fix(gatsby-plugin-sitemap): add meaningful error when siteUrl is miss…
Browse files Browse the repository at this point in the history
…ing (gatsbyjs#13123)

## Description
Throw a more descriptive error when users do not supply a `siteUrl` property when using `gatsby-plugin-sitemap`

## Related Issues
Fixes gatsbyjs#12912
  • Loading branch information
sbardian authored and johno committed Apr 10, 2019
1 parent e94ecc4 commit 95270b3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
14 changes: 14 additions & 0 deletions packages/gatsby-plugin-sitemap/src/__tests__/internals.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`, () => {
Expand Down
12 changes: 8 additions & 4 deletions packages/gatsby-plugin-sitemap/src/internals.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
})

Expand Down

0 comments on commit 95270b3

Please sign in to comment.