diff --git a/src/components/shared/seo/seo.view.js b/src/components/shared/seo/seo.view.js index 6dac3ad3a3..a225d33617 100644 --- a/src/components/shared/seo/seo.view.js +++ b/src/components/shared/seo/seo.view.js @@ -40,14 +40,35 @@ export const SEO = ({ const currentUrl = slug ? `${docs}/${slug}` : docs; const currentImage = createMetaImagePath(image, siteUrl, siteImage); - const hrefLangRelAttributeHash = { - en: 'alternate', - es: 'alternate', + const hrefLangAttributes = { + en: { rel: 'alternate', href: `${siteUrl}/docs` }, + es: { rel: 'alternate', href: `${siteUrl}/docs` }, }; + + if (pageTranslations) { + let esPathname = pageTranslations.es.path; + let enPathname = pageTranslations.en.path; + + if (esPathname.endsWith('/')) { + // gatsby-plugin-react-helmet-canonical-urls.noTrailingSlash + esPathname = esPathname.substring(0, esPathname.length - 1); + } + if (enPathname.endsWith('/')) { + // gatsby-plugin-react-helmet-canonical-urls.noTrailingSlash + enPathname = enPathname.substring(0, enPathname.length - 1); + } + hrefLangAttributes.es.href += esPathname; + hrefLangAttributes.en.href += enPathname; + + // if no pageTranslations, we must enable `gatsby-plugin-react-helmet-canonical-urls` + } + + // we have to setup the `canonical` URL with `hreflang`. + // we must do it here because `gatsby-plugin-react-helmet-canonical-urls` does not cover this case if (slug && (slug.startsWith('es/') || slug === 'es')) { - hrefLangRelAttributeHash.es = 'canonical'; + hrefLangAttributes.es.rel = 'canonical'; } else { - hrefLangRelAttributeHash.en = 'canonical'; + hrefLangAttributes.en.rel = 'canonical'; } return ( @@ -82,21 +103,21 @@ export const SEO = ({ {pageTranslations && pageTranslations.en !== undefined && ( )} {pageTranslations && pageTranslations.es !== undefined && ( )} {pageTranslations && ( )}