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

Cannot build gatsby sitemap #199

Closed
PanadeEdu opened this issue Apr 28, 2019 · 6 comments

Comments

Projects
None yet
5 participants
@PanadeEdu
Copy link

commented Apr 28, 2019

Describe the bug
When trying to build the app for production with gatsby build && gatsby serve, the console output would throw the following error

error Plugin gatsby-plugin-sitemap returned an error


  Error: SiteMetaData 'siteUrl' property is required. Check out the documentatio  n to see a working example: https://www.gatsbyjs.org/packages/gatsby-plugin-si  temap/#how-to-use

To Reproduce
Steps to reproduce the behavior:

  1. Create new starter instance: gatsby new blog https://github.com/alxshelepenok/gatsby-starter-lumen
  2. Go to blog directory
  3. Execute gatsby build && gatsby serve
  4. See error
    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

Expected behavior
gatsby build && gatsby serve should run without any issues and build the sitemap properly.

Screenshots
commandline issue

Desktop (please complete the following information):

  • OS: Ubuntu 18.04 LTS
  • Browser: none (terminal)
  • Versions:
    npm: 6.0.0
    node: 8.8.1
    gatsby: 2.5.8

Additional context
Even tho I know this is meant to be served using Netlify, I think this should be able to be used standalone as well.
I went trough a search for the issue and found this: gatsbyjs/gatsby#9036 which states that sitemap expects siteUrl to be set.

I was able to solve this issue in my local installation but figured it makes sense to share it here as a bug.
Since I am new to Gatsby I am not 100% sure if this variable is used elsewhere, so my solution may not be optimal.

Proposed solution (worked for me)
In the gatsby-config.js module.exports add siteUrl: siteConfig.url inside the siteMetadata object.

siteMetadata: {
    url: siteConfig.url,
    siteUrl: siteConfig.url,
    title: siteConfig.title,
    subtitle: siteConfig.subtitle,
    copyright: siteConfig.copyright,
    legalMenu: siteConfig.legalMenu,
    disqusShortname: siteConfig.disqusShortname,
    menu: siteConfig.menu,
    author: siteConfig.author
  },

Then go to plugin config for gatsby-plugin-sitemap and change siteMetadata read to siteUrl instead of url. Also in the serialize part, make it read site.siteMetadata.siteUrl instead of site.siteMetadata.url.

{
      resolve: 'gatsby-plugin-sitemap',
      options: {
        query: `
          {
            site {
              siteMetadata {
                siteUrl
              }
            }
            allSitePage(
              filter: {
                path: { regex: "/^(?!/404/|/404.html|/dev-404-page/)/" }
              }
            ) {
              edges {
                node {
                  path
                }
              }
            }
          }
        `,
        output: '/sitemap.xml',
        serialize: ({ site, allSitePage }) => allSitePage.edges.map((edge) => ({
          url: site.siteMetadata.siteUrl + edge.node.path,
          changefreq: 'daily',
          priority: 0.7
        }))
      }
    },
@ngl4

This comment has been minimized.

Copy link

commented Apr 29, 2019

For the solution, I actually did the same thing as you did for the plugin config. The only difference is, instead of using siteUrl: siteConfig.url, I replace siteConfig.url with the actual Netlify hosted url, siteUrl: www.example.netlify.com

@PanadeEdu

This comment has been minimized.

Copy link
Author

commented Apr 29, 2019

@ngl4 I may have seen something in the rss plugin that utilizes url as well. Also, the point of a config is to have it in one place which is generally not bad. What siteUrl: siteConfig.url does is just utilizing the variable loaded in config.js.

If it works for you, its fine, just want to mention that you split now the places you actually edit your config. Ideally you want all data to be configured in just one file.

@ascherj

This comment has been minimized.

Copy link

commented Apr 29, 2019

@PanadeEdu I just encountered this same issue. Implemented your proposed solution and it appears to be working for me. Thanks!

@jjosseff

This comment has been minimized.

Copy link

commented Apr 29, 2019

Hi, thanks for the hint, it helped me to get rid of one issue and understand more about configs in gatsby. But now when building I get the error "Plugin gatsby-plugin-sitemap returned an error
Protocol is required". Any ideas what is missing now? Sorry if I am asking in a wrong place.

@PanadeEdu

This comment has been minimized.

Copy link
Author

commented Apr 29, 2019

@jjosseff I ran into the same issue. Debugging the sitemap node module itself, showed me that this happens also when the variable siteUrl is undefined.

Check the graphql query and the stream of data. Most likely it is a typo or something missing.

In my case, I forgot about the part below the graphql query. In the serialize you also have to change url to use siteUrl.

@jjosseff

This comment has been minimized.

Copy link

commented Apr 29, 2019

@PanadeEdu Thank you!!! You are ⭐️! Indeed, after changing "url" to "siteUrl" in serialize section in gatsby-config.js, all works ok! 🎉

alxshelepenok added a commit that referenced this issue May 10, 2019

thoward27 added a commit to thoward27/website that referenced this issue May 10, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.