microapps / gatsby-plugin-react-i18next

Easily translate your Gatsby website into multiple languages
MIT License
121 stars 72 forks source link

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

Open neatbyte-vnobis opened 2 years ago

neatbyte-vnobis commented 2 years ago

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?