gatsby: 'gatsby-plugin-sitemap' doesn't generate a /sitemap.xml file

Description

‘gatsby-plugin-sitemap’ doesn’t generate a sitemap.xml file, even with default config.

I’ve changed to the default config of this plugin. In the past it worked like a charm with this code (beneath) but since gatsby-plugin-sitemap v4 it stopped working. Now even the default config doesn’t show me the sitemap by going to ‘yoursiteurl.com/sitemap.xml’.

{
      resolve: 'gatsby-plugin-sitemap',
      options: {
        query: `
        {
          site {
            siteMetadata {
              siteUrl
            }
          }

          allSitePage {
            edges {
              node {
                path
                context {
                  updatedAt
                }
              }
            }
          }
      }`,
        serialize: ({ site, allSitePage }) =>
          allSitePage.edges.map((edge) => ({
            url: `${site.siteMetadata.siteUrl}${edge.node.path}`,
            changefreq: 'daily',
            priority: 0.7,
            lastmodISO: edge.node.context.updatedAt,
          })),
      },
    },

or

plugins: [`gatsby-plugin-sitemap`]

The old config gives me serializing errors and the second (default) one gives me no sitemap, but goes to 404. What am i doing wrong??

Expected result

Sitemap should show up when I gatsby build && gatsby serve and go to localhost:9000/sitemap.xml (‘localhost:9000/sitemap’ isn’t working either)

Actual result

No sitemap to be found…

Environment


  System:
    OS: macOS 11.3
    CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 15.14.0 - ~/.nvm/versions/node/v15.14.0/bin/node
    Yarn: 1.22.10 - /usr/local/bin/yarn
    npm: 7.11.1 - ~/.nvm/versions/node/v15.14.0/bin/npm
  Languages:
    Python: 2.7.16 - /usr/bin/python
  Browsers:
    Chrome: 90.0.4430.93
    Safari: 14.1
  npmPackages:
    gatsby: ^3.4.0 => 3.4.0 
    gatsby-plugin-canonical-urls: ^3.4.0 => 3.4.0 
    gatsby-plugin-catch-links: ^3.4.0 => 3.4.0 
    gatsby-plugin-eslint: ^3.0.0 => 3.0.0 
    gatsby-plugin-feed: ^3.4.0 => 3.4.0 
    gatsby-plugin-google-tagmanager: ^3.4.0 => 3.4.0 
    gatsby-plugin-image: ^1.4.0 => 1.4.0 
    gatsby-plugin-manifest: ^3.4.0 => 3.4.0 
    gatsby-plugin-netlify: ^3.4.0 => 3.4.0 
    gatsby-plugin-nprogress: ^3.4.0 => 3.4.0 
    gatsby-plugin-offline: ^4.4.0 => 4.4.0 
    gatsby-plugin-react-helmet: ^4.4.0 => 4.4.0 
    gatsby-plugin-robots-txt: ^1.5.6 => 1.5.6 
    gatsby-plugin-sass: ^4.4.0 => 4.4.0 
    gatsby-plugin-sharp: ^3.4.0 => 3.4.0 
    gatsby-plugin-sitemap: ^4.0.0 => 4.0.0 
    gatsby-plugin-smoothscroll: ^1.2.0 => 1.2.0 
    gatsby-remark-images: ^5.1.0 => 5.1.0 
    gatsby-remark-images-contentful: ^4.1.0 => 4.1.0 
    gatsby-remark-relative-images: ^2.0.2 => 2.0.2 
    gatsby-source-contentful: ^5.4.0 => 5.4.0 
    gatsby-source-filesystem: ^3.4.0 => 3.4.0 
    gatsby-transformer-remark: ^4.1.0 => 4.1.0 
    gatsby-transformer-sharp: ^3.4.0 => 3.4.0 
  npmGlobalPackages:
    gatsby-cli: 3.4.0

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 4
  • Comments: 20 (8 by maintainers)

Most upvoted comments

My understanding is that the gatsby-plugin-sitemap version 4.0.0 is generating the files under a /sitemap folder (that’s where I see the files now)

Check the html headers in your generated files, search for “sitemap” and you will see the new path.

Having said that, I still think there must be some bug, as the index file has “<domain>/sitemap-0.xml” instead of the expected “<domain>/sitemap/sitemap-0.xml”

I’ve tried setting “output” to “” in the config, but apparently, gatsby doesn’t allow generating the sitemaps on the root now…

I’m also having issue with the sitemap plugin with Error: Page array from `query` wasn't found at `data.allSitePage.nodes`.

I can confirm that reverting to 3.3.0 fixes the issue.

Careful about the breaking change from options.exclude to options.excludes that isn’t mentioned anywhere…

I’ve submitted a small PR that will allow setting output: "" in the options so that the sitemap files are generated on the root on the public folder just as it did before! Let’s hope someone notices and merges it soon! 🙂

I’ve putted this together with the v4 documentation, and its working like before…

{
      resolve: 'gatsby-plugin-sitemap',
      options: {
        query: `
        {
          allSitePage {
            nodes {
              path
              context {
                updatedAt
              }
            }
          }
          allContentfulBlogPost {
            nodes {
              ... on ContentfulBlogPost {
                slug
              }
            }
          }
        }
      `,
        resolveSiteUrl: () => siteUrl,
        resolvePages: ({
          allSitePage: { nodes: allPages },
          allContentfulBlogPost: { nodes: allCfNodes },
        }) => {
          const cfNodeMap = allCfNodes.reduce((acc, node) => {
            const { slug } = node;
            acc[slug] = node;

            return acc;
          }, {});

          return allPages.map((page) => ({ ...page, ...cfNodeMap[page.path] }));
        },
        serialize: ({ path, context }) => ({
          url: path,
          changefreq: 'daily',
          priority: 0.7,
          lastmod: context.updatedAt,
        }),
      },
    },

Hope this helps you out as well guys, PS: i’m still looking for the simple /sitemap.xml slug option… but this gets me a lot further.

I’m also having issue with the sitemap plugin with Error: Page array from `query` wasn't found at `data.allSitePage.nodes`.

I had the same issue, it was saying something about adding a ‘resolvePages’ function

Using output: "/" is not ideal as the header gets generated with “//sitemap-index.xml”… not sure what is the reasoning to now allow output: "", but that would probably fix the whole thing!

I just tried removing my node_modules and package-lock.json for a fresh install and the problem still persists. 😢

If it’s any help i’ve added my environment below:

  System:
    OS: Windows 10 10.0.19042
    CPU: (6) x64 Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz
  Binaries:
    Node: 15.10.0 - C:\Program Files\nodejs\node.EXE
    npm: 7.11.1 - C:\Program Files\nodejs\npm.CMD
  Languages:
    Python: 3.9.2 - /c/Python39/python
  Browsers:
    Chrome: 90.0.4430.93
    Edge: Spartan (44.19041.423.0), Chromium (90.0.818.49)
  npmPackages:
    gatsby: 3.4.0 => 3.4.0
    gatsby-plugin-gdpr-cookies: ^2.0.0 => 2.0.0
    gatsby-plugin-htaccess: ^1.4.0 => 1.4.0
    gatsby-plugin-image: 1.4.0 => 1.4.0
    gatsby-plugin-manifest: ^3.4.0 => 3.4.0
    gatsby-plugin-react-helmet: ^4.4.0 => 4.4.0
    gatsby-plugin-robots-txt: ^1.5.6 => 1.5.6
    gatsby-plugin-sharp: ^3.4.0 => 3.4.0
    gatsby-plugin-sitemap: 4.0.0 => 4.0.0
    gatsby-plugin-styled-components: ^4.4.0 => 4.4.0
    gatsby-source-filesystem: ^3.4.0 => 3.4.0
    gatsby-source-wordpress: ^5.4.0 => 5.4.0
    gatsby-transformer-sharp: ^3.4.0 => 3.4.0
  npmGlobalPackages:
    gatsby-cli: 3.4.0

I’ve also experienced this issue after upgrading to 4.0.0. In version 3.3.0 using plugins: ['gatsby-plugin-sitemap'] would generate a sitemap.xml file in the public directory but in 4.0.0 nothing is created and there are no errors.

I also tried the example config from the docs which again returned no errors but did not create a file.

The plugin code appears to run correctly as a console.log inside the example prints out as expected