Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gatsby-plugin-sitemap): Properly throw error on missing siteUrl #31963

Merged
merged 1 commit into from
Jun 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/gatsby-plugin-sitemap/README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixing indentation in this file

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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
module.exports = {
siteMetadata: {
siteUrl: `https://www.yourdomain.tld`,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create-gatsby doesn't have the ability (yet) to define siteMetadata data for each plugin so for now this must be in here for everything (even when doesn't use sitemap plugin)

},
plugins: [

]
Expand Down