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

Not working "sitemap.xml for all language pages" sample #153

Open
neatbyte-vnobis opened this issue Aug 30, 2022 · 0 comments
Open

Not working "sitemap.xml for all language pages" sample #153

neatbyte-vnobis opened this issue Aug 30, 2022 · 0 comments

Comments

@neatbyte-vnobis
Copy link

The example "How to add sitemap.xml for all language specific pages" here is not working and giving error:

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

Gatsby version - "4.19.0"
"gatsby-plugin-sitemap" - "5.20.0"
"gatsby-plugin-react-i18next" - "2.0.1"

It can be fixed in this way:

// required as a serialize func don't get "siteUrl" value from a query
const siteUrl = `https://example.com`;

...

// plugin config
    {
      resolve: "gatsby-plugin-sitemap",
      options: {
        excludes: ["/**/404", "/**/404.html"],
        query: `
            {
              site {
                siteMetadata {
                  siteUrl
                }
              }
              allSitePage(filter: {context: {i18n: {routed: {eq: false}}}}) {
                nodes {
                  context {
                    i18n {
                      defaultLanguage
                      languages
                      originalPath
                    }
                  }
                  path
                }
              }
            }
          `,
        serialize: (page) => {
          const { languages, originalPath, defaultLanguage } = page.context.i18n;
          const url = `${siteUrl}${originalPath}`;
          const links = [
            { lang: defaultLanguage, url },
            { lang: "x-default", url },
          ];
          languages.forEach((lang) => {
            if (lang === defaultLanguage) return;
            links.push({ lang, url: `${siteUrl}/${lang}${originalPath}` });
          });
          return {
            url,
            changefreq: "daily",
            priority: originalPath === "/" ? 1.0 : 0.7,
            links,
          };
        },
      },
    }

Maybe demo Gatsby project and readme code sample are outdated?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant