Skip to content

Commit f91dd52

Browse files
authored
fix(gatsby-plugin-sitemap): Properly throw error on missing siteUrl (#31963)
1 parent d469867 commit f91dd52

File tree

5 files changed

+31
-19
lines changed

5 files changed

+31
-19
lines changed

packages/gatsby-plugin-sitemap/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ _NOTE: This plugin only generates output when run in `production` mode! To test
1313
```javascript
1414
// In your gatsby-config.js
1515
siteMetadata: {
16+
// If you didn't use the resolveSiteUrl option this needs to be set
1617
siteUrl: `https://www.example.com`,
1718
},
1819
plugins: [`gatsby-plugin-sitemap`]

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

+15-8
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,28 @@ exports.onPostBuild = async (
1919
serialize,
2020
}
2121
) => {
22-
const { data: queryRecords } = await graphql(query)
23-
24-
// resolvePages and resolveSuteUrl are allowed to be sync or async. The Promise.resolve handles each possibility
25-
const allPages = await Promise.resolve(
26-
resolvePages(queryRecords)
27-
).catch(err =>
28-
reporter.panic(`${REPORTER_PREFIX} Error resolving Pages`, err)
29-
)
22+
const { data: queryRecords, errors } = await graphql(query)
3023

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

31+
if (errors) {
32+
reporter.panic(
33+
`Error executing the GraphQL query inside gatsby-plugin-sitemap:\n`,
34+
errors
35+
)
36+
}
37+
38+
const allPages = await Promise.resolve(
39+
resolvePages(queryRecords)
40+
).catch(err =>
41+
reporter.panic(`${REPORTER_PREFIX} Error resolving Pages`, err)
42+
)
43+
3744
if (!Array.isArray(allPages)) {
3845
reporter.panic(
3946
`${REPORTER_PREFIX} The \`resolvePages\` function did not return an array.`

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

+11-11
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ export function resolveSiteUrl(data) {
3434
if (!data?.site?.siteMetadata?.siteUrl) {
3535
throw Error(
3636
`\`siteUrl\` does not exist on \`siteMetadata\` in the data returned from the query.
37-
Add this to your custom query or provide a custom \`resolveSiteUrl\` function.
38-
https://www.gatsbyjs.com/plugins/gatsby-plugin-sitemap/#api-reference
37+
Add this to your \`siteMetadata\` object inside gatsby-config.js or add this to your custom query or provide a custom \`resolveSiteUrl\` function.
38+
https://www.gatsbyjs.com/plugins/gatsby-plugin-sitemap/#api-reference
3939
`
4040
)
4141
}
@@ -56,8 +56,8 @@ export function resolvePagePath(page) {
5656
if (!page?.path) {
5757
throw Error(
5858
`\`path\` does not exist on your page object.
59-
Make the page URI available at \`path\` or provide a custom \`resolvePagePath\` function.
60-
https://www.gatsbyjs.com/plugins/gatsby-plugin-sitemap/#api-reference
59+
Make the page URI available at \`path\` or provide a custom \`resolvePagePath\` function.
60+
https://www.gatsbyjs.com/plugins/gatsby-plugin-sitemap/#api-reference
6161
`
6262
)
6363
}
@@ -78,8 +78,8 @@ export function resolvePages(data) {
7878
if (!data?.allSitePage?.nodes) {
7979
throw Error(
8080
`Page array from \`query\` wasn't found at \`data.allSitePage.nodes\`.
81-
Fix the custom query or provide a custom \`resolvePages\` function.
82-
https://www.gatsbyjs.com/plugins/gatsby-plugin-sitemap/#api-reference
81+
Fix the custom query or provide a custom \`resolvePages\` function.
82+
https://www.gatsbyjs.com/plugins/gatsby-plugin-sitemap/#api-reference
8383
`
8484
)
8585
}
@@ -109,8 +109,8 @@ export function defaultFilterPages(
109109
if (typeof excludedRoute !== `string`) {
110110
throw new Error(
111111
`You've passed something other than string to the exclude array. This is supported, but you'll have to write a custom filter function.
112-
Ignoring the input for now: ${JSON.stringify(excludedRoute, null, 2)}
113-
https://www.gatsbyjs.com/plugins/gatsby-plugin-sitemap/#api-reference
112+
Ignoring the input for now: ${JSON.stringify(excludedRoute, null, 2)}
113+
https://www.gatsbyjs.com/plugins/gatsby-plugin-sitemap/#api-reference
114114
`
115115
)
116116
}
@@ -203,9 +203,9 @@ export function pageFilter({ allPages, filterPages, excludes }) {
203203
} catch {
204204
throw new Error(
205205
`${REPORTER_PREFIX} Error in custom page filter.
206-
If you've customized your excludes you may need to provide a custom "filterPages" function in your config.
207-
https://www.gatsbyjs.com/plugins/gatsby-plugin-sitemap/#api-reference
208-
`
206+
If you've customized your excludes you may need to provide a custom "filterPages" function in your config.
207+
https://www.gatsbyjs.com/plugins/gatsby-plugin-sitemap/#api-reference
208+
`
209209
)
210210
}
211211
})

starters/default/gatsby-config.js

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module.exports = {
33
title: `Gatsby Default Starter`,
44
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.`,
55
author: `@gatsbyjs`,
6+
siteUrl: `https://gatsbystarterdefaultsource.gatsbyjs.io/`,
67
},
78
plugins: [
89
`gatsby-plugin-react-helmet`,

starters/gatsby-starter-minimal/gatsby-config.js

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
module.exports = {
2+
siteMetadata: {
3+
siteUrl: `https://www.yourdomain.tld`,
4+
},
25
plugins: [
36

47
]

0 commit comments

Comments
 (0)