From cd7f85bf805e24b9dbd34dc64bfdd690235deb27 Mon Sep 17 00:00:00 2001 From: Brian Andrews Date: Thu, 4 Apr 2019 09:20:30 -0600 Subject: [PATCH 1/4] Throw error when siteUrl property not found --- packages/gatsby-plugin-sitemap/src/internals.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/gatsby-plugin-sitemap/src/internals.js b/packages/gatsby-plugin-sitemap/src/internals.js index 3be0785849ef2..6b104167b692c 100644 --- a/packages/gatsby-plugin-sitemap/src/internals.js +++ b/packages/gatsby-plugin-sitemap/src/internals.js @@ -29,6 +29,10 @@ export const runQuery = (handler, query, excludes, pathPrefix) => return page }) + if (!r.data.site.siteMetadata.siteUrl) { + throw new Error(`SiteMetaData 'siteUrl' property is required`) + } + if (r.data.site.siteMetadata.siteUrl) { // remove trailing slash of siteUrl r.data.site.siteMetadata.siteUrl = withoutTrailingSlash( From d23d69ca353ff940d7332e6c1cdc30e022ba7b83 Mon Sep 17 00:00:00 2001 From: Brian Andrews Date: Thu, 4 Apr 2019 09:27:47 -0600 Subject: [PATCH 2/4] Add note in README that siteUrl is required --- packages/gatsby-plugin-sitemap/README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/gatsby-plugin-sitemap/README.md b/packages/gatsby-plugin-sitemap/README.md index e3c6a55f47cf6..d6bd0db4a3fd9 100644 --- a/packages/gatsby-plugin-sitemap/README.md +++ b/packages/gatsby-plugin-sitemap/README.md @@ -21,6 +21,10 @@ plugins: [`gatsby-plugin-sitemap`] Above is the minimal configuration required to have it work. By default, the generated sitemap will include all of your site's pages, except the ones you exclude. +## Note + +`siteUrl` is required with `gatsby-plugin-sitemap`. + ## Options The `defaultOptions` [here](https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-plugin-sitemap/src/internals.js#L34) can be overridden. From f3b92353b24a443c24c3709ed8b61dd675b9037e Mon Sep 17 00:00:00 2001 From: Brian Andrews Date: Thu, 4 Apr 2019 14:20:28 -0600 Subject: [PATCH 3/4] Remove duplicate null check and add link to docs --- packages/gatsby-plugin-sitemap/src/internals.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/gatsby-plugin-sitemap/src/internals.js b/packages/gatsby-plugin-sitemap/src/internals.js index 6b104167b692c..38bcb83236652 100644 --- a/packages/gatsby-plugin-sitemap/src/internals.js +++ b/packages/gatsby-plugin-sitemap/src/internals.js @@ -30,16 +30,16 @@ export const runQuery = (handler, query, excludes, pathPrefix) => }) if (!r.data.site.siteMetadata.siteUrl) { - throw new Error(`SiteMetaData 'siteUrl' property is required`) - } - - if (r.data.site.siteMetadata.siteUrl) { - // remove trailing slash of siteUrl - r.data.site.siteMetadata.siteUrl = withoutTrailingSlash( - r.data.site.siteMetadata.siteUrl + throw new Error( + `SiteMetaData 'siteUrl' property is required. (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 }) From 9f79efabdc5fceec1bd9d6b532bbb3225bcff669 Mon Sep 17 00:00:00 2001 From: Ward Peeters Date: Fri, 5 Apr 2019 12:34:29 +0200 Subject: [PATCH 4/4] fix review & add test --- packages/gatsby-plugin-sitemap/README.md | 4 ---- .../src/__tests__/internals.js | 14 ++++++++++++++ packages/gatsby-plugin-sitemap/src/internals.js | 2 +- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/packages/gatsby-plugin-sitemap/README.md b/packages/gatsby-plugin-sitemap/README.md index d6bd0db4a3fd9..e3c6a55f47cf6 100644 --- a/packages/gatsby-plugin-sitemap/README.md +++ b/packages/gatsby-plugin-sitemap/README.md @@ -21,10 +21,6 @@ plugins: [`gatsby-plugin-sitemap`] Above is the minimal configuration required to have it work. By default, the generated sitemap will include all of your site's pages, except the ones you exclude. -## Note - -`siteUrl` is required with `gatsby-plugin-sitemap`. - ## Options The `defaultOptions` [here](https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-plugin-sitemap/src/internals.js#L34) can be overridden. 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 38bcb83236652..72b9b75ed336c 100644 --- a/packages/gatsby-plugin-sitemap/src/internals.js +++ b/packages/gatsby-plugin-sitemap/src/internals.js @@ -31,7 +31,7 @@ export const runQuery = (handler, query, excludes, pathPrefix) => if (!r.data.site.siteMetadata.siteUrl) { throw new Error( - `SiteMetaData 'siteUrl' property is required. (https://www.gatsbyjs.org/packages/gatsby-plugin-sitemap/#how-to-use)` + `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` ) }