Gatsby: Multi language support for sitemap

Created on 19 Mar 2018  ·  6Comments  ·  Source: gatsbyjs/gatsby

Description

I propose to discuss in which way will be better implement multi language support in gatsby-plugin-sitemap. One of the way, is implement in a way how to google proposed in this article
https://support.google.com/webmasters/answer/2620865?hl=en

stale? awaiting author response needs more info

Most helpful comment

Heya! For those stumbling across this issue looking to internationalize a sitemap of their own, I do have a bit of good news 😄gatsby-plugin-sitemap actually does support internationalized sitemaps. It just takes a little bit of extra code.

Let's say your website is https://sitemaps.com and your Spanish version is https://sitemaps.com/es, you will want to add the links property with an array of three alternative links: English, Spanish, and a default for a fall-back in case the page hasn't been translated to the user's preferred language just yet.

Here's how that works:

// In your gatsby-config.js
siteMetadata: {
  siteUrl: `https://sitemaps.com`,
},
plugins: [
  {
    resolve: `gatsby-plugin-sitemap`,
    options: {
      query: `
        {
          site {
            siteMetadata {
              siteUrl
            }
          }

          allSitePage {
            edges {
              node {
                path
              }
            }
          }
      }`,
      serialize: ({ site, allSitePage }) => allSitePage.edges
        .map(edge => {
          return {
            url: site.siteMetadata.siteUrl + path, // https://sitemaps.com/page-path
            changefreq: 'daily',
            priority: 0.7,
            links: [
              // https://sitemaps.com/page-path
              { lang: 'en', url: site.siteMetadata.siteUrl + path },
              // https://sitemaps.com/es/page-path
              { lang: 'es', url: `${site.siteMetadata.siteUrl}/es${path}` },
              // The default in case page for user's language is not localized.
              { lang: 'x-default', url: site.siteMetadata.siteUrl + path } 
            ]
          };
        })
    }
  }
]

With this good news, I bring a slight round of bad news 🤷‍♂️ This will cause most web-viewers to not be able to pretty print the sitemap anymore. For an in-depth reason as to why, check out How to indicate alternate languages pages?. The sitemap is still a valid sitemap, however.

All 6 comments

Hey @KikahaPlus44, sounds great! Would you like to put together a PR so we can look in more detail at what you're proposing?

@KikahaPlus44 would you like to work on this and create a PR for the same?

Old issues will be closed after 30 days of inactivity. This issue has been quiet for 20 days and is being marked as stale. Reply here or add the label "not stale" to keep this issue open!

Heya! For those stumbling across this issue looking to internationalize a sitemap of their own, I do have a bit of good news 😄gatsby-plugin-sitemap actually does support internationalized sitemaps. It just takes a little bit of extra code.

Let's say your website is https://sitemaps.com and your Spanish version is https://sitemaps.com/es, you will want to add the links property with an array of three alternative links: English, Spanish, and a default for a fall-back in case the page hasn't been translated to the user's preferred language just yet.

Here's how that works:

// In your gatsby-config.js
siteMetadata: {
  siteUrl: `https://sitemaps.com`,
},
plugins: [
  {
    resolve: `gatsby-plugin-sitemap`,
    options: {
      query: `
        {
          site {
            siteMetadata {
              siteUrl
            }
          }

          allSitePage {
            edges {
              node {
                path
              }
            }
          }
      }`,
      serialize: ({ site, allSitePage }) => allSitePage.edges
        .map(edge => {
          return {
            url: site.siteMetadata.siteUrl + path, // https://sitemaps.com/page-path
            changefreq: 'daily',
            priority: 0.7,
            links: [
              // https://sitemaps.com/page-path
              { lang: 'en', url: site.siteMetadata.siteUrl + path },
              // https://sitemaps.com/es/page-path
              { lang: 'es', url: `${site.siteMetadata.siteUrl}/es${path}` },
              // The default in case page for user's language is not localized.
              { lang: 'x-default', url: site.siteMetadata.siteUrl + path } 
            ]
          };
        })
    }
  }
]

With this good news, I bring a slight round of bad news 🤷‍♂️ This will cause most web-viewers to not be able to pretty print the sitemap anymore. For an in-depth reason as to why, check out How to indicate alternate languages pages?. The sitemap is still a valid sitemap, however.

This issue is being closed due to inactivity. Is this a mistake? Please re-open this issue or create a new issue.

any workaround how can we fix pretty print for sitemap?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

3CordGuy picture 3CordGuy  ·  3Comments

theduke picture theduke  ·  3Comments

mikestopcontinues picture mikestopcontinues  ·  3Comments

dustinhorton picture dustinhorton  ·  3Comments

magicly picture magicly  ·  3Comments