Skip to content

Commit

Permalink
fix(gatsby-plugin-sitemap): Properly throw error on missing siteUrl (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
LekoArts committed Jun 18, 2021
1 parent d469867 commit f91dd52
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 19 deletions.
1 change: 1 addition & 0 deletions packages/gatsby-plugin-sitemap/README.md
Expand Up @@ -13,6 +13,7 @@ _NOTE: This plugin only generates output when run in `production` mode! To test
```javascript
// In your gatsby-config.js
siteMetadata: {
// If you didn't use the resolveSiteUrl option this needs to be set
siteUrl: `https://www.example.com`,
},
plugins: [`gatsby-plugin-sitemap`]
Expand Down
23 changes: 15 additions & 8 deletions packages/gatsby-plugin-sitemap/src/gatsby-node.js
Expand Up @@ -19,21 +19,28 @@ exports.onPostBuild = async (
serialize,
}
) => {
const { data: queryRecords } = await graphql(query)

// resolvePages and resolveSuteUrl are allowed to be sync or async. The Promise.resolve handles each possibility
const allPages = await Promise.resolve(
resolvePages(queryRecords)
).catch(err =>
reporter.panic(`${REPORTER_PREFIX} Error resolving Pages`, err)
)
const { data: queryRecords, errors } = await graphql(query)

// resolvePages and resolveSiteUrl are allowed to be sync or async. The Promise.resolve handles each possibility
const siteUrl = await Promise.resolve(
resolveSiteUrl(queryRecords)
).catch(err =>
reporter.panic(`${REPORTER_PREFIX} Error resolving Site URL`, err)
)

if (errors) {
reporter.panic(
`Error executing the GraphQL query inside gatsby-plugin-sitemap:\n`,
errors
)
}

const allPages = await Promise.resolve(
resolvePages(queryRecords)
).catch(err =>
reporter.panic(`${REPORTER_PREFIX} Error resolving Pages`, err)
)

if (!Array.isArray(allPages)) {
reporter.panic(
`${REPORTER_PREFIX} The \`resolvePages\` function did not return an array.`
Expand Down
22 changes: 11 additions & 11 deletions packages/gatsby-plugin-sitemap/src/internals.js
Expand Up @@ -34,8 +34,8 @@ export function resolveSiteUrl(data) {
if (!data?.site?.siteMetadata?.siteUrl) {
throw Error(
`\`siteUrl\` does not exist on \`siteMetadata\` in the data returned from the query.
Add this to your custom query or provide a custom \`resolveSiteUrl\` function.
https://www.gatsbyjs.com/plugins/gatsby-plugin-sitemap/#api-reference
Add this to your \`siteMetadata\` object inside gatsby-config.js or add this to your custom query or provide a custom \`resolveSiteUrl\` function.
https://www.gatsbyjs.com/plugins/gatsby-plugin-sitemap/#api-reference
`
)
}
Expand All @@ -56,8 +56,8 @@ export function resolvePagePath(page) {
if (!page?.path) {
throw Error(
`\`path\` does not exist on your page object.
Make the page URI available at \`path\` or provide a custom \`resolvePagePath\` function.
https://www.gatsbyjs.com/plugins/gatsby-plugin-sitemap/#api-reference
Make the page URI available at \`path\` or provide a custom \`resolvePagePath\` function.
https://www.gatsbyjs.com/plugins/gatsby-plugin-sitemap/#api-reference
`
)
}
Expand All @@ -78,8 +78,8 @@ export function resolvePages(data) {
if (!data?.allSitePage?.nodes) {
throw Error(
`Page array from \`query\` wasn't found at \`data.allSitePage.nodes\`.
Fix the custom query or provide a custom \`resolvePages\` function.
https://www.gatsbyjs.com/plugins/gatsby-plugin-sitemap/#api-reference
Fix the custom query or provide a custom \`resolvePages\` function.
https://www.gatsbyjs.com/plugins/gatsby-plugin-sitemap/#api-reference
`
)
}
Expand Down Expand Up @@ -109,8 +109,8 @@ export function defaultFilterPages(
if (typeof excludedRoute !== `string`) {
throw new Error(
`You've passed something other than string to the exclude array. This is supported, but you'll have to write a custom filter function.
Ignoring the input for now: ${JSON.stringify(excludedRoute, null, 2)}
https://www.gatsbyjs.com/plugins/gatsby-plugin-sitemap/#api-reference
Ignoring the input for now: ${JSON.stringify(excludedRoute, null, 2)}
https://www.gatsbyjs.com/plugins/gatsby-plugin-sitemap/#api-reference
`
)
}
Expand Down Expand Up @@ -203,9 +203,9 @@ export function pageFilter({ allPages, filterPages, excludes }) {
} catch {
throw new Error(
`${REPORTER_PREFIX} Error in custom page filter.
If you've customized your excludes you may need to provide a custom "filterPages" function in your config.
https://www.gatsbyjs.com/plugins/gatsby-plugin-sitemap/#api-reference
`
If you've customized your excludes you may need to provide a custom "filterPages" function in your config.
https://www.gatsbyjs.com/plugins/gatsby-plugin-sitemap/#api-reference
`
)
}
})
Expand Down
1 change: 1 addition & 0 deletions starters/default/gatsby-config.js
Expand Up @@ -3,6 +3,7 @@ module.exports = {
title: `Gatsby Default Starter`,
description: `Kick off your next, great Gatsby project with this default starter. This barebones starter ships with the main Gatsby configuration files you might need.`,
author: `@gatsbyjs`,
siteUrl: `https://gatsbystarterdefaultsource.gatsbyjs.io/`,
},
plugins: [
`gatsby-plugin-react-helmet`,
Expand Down
3 changes: 3 additions & 0 deletions starters/gatsby-starter-minimal/gatsby-config.js
@@ -1,4 +1,7 @@
module.exports = {
siteMetadata: {
siteUrl: `https://www.yourdomain.tld`,
},
plugins: [

]
Expand Down

0 comments on commit f91dd52

Please sign in to comment.