Skip to content

Commit

Permalink
Fix canonical attribute when using hreflang
Browse files Browse the repository at this point in the history
  • Loading branch information
ppcano committed Mar 31, 2021
1 parent c9cfb42 commit a838a02
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
18 changes: 11 additions & 7 deletions gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@ const shouldAnnouncementBannerBeShown = false;

const plugins = [
'gatsby-plugin-react-helmet',
{
resolve: `gatsby-plugin-react-helmet-canonical-urls`,
options: {
siteUrl: process.env.GATSBY_DEFAULT_MAIN_URL,
noTrailingSlash: true,
},
},
'gatsby-transformer-sharp',
'gatsby-plugin-sharp',
'gatsby-plugin-catch-links',
Expand Down Expand Up @@ -171,6 +164,17 @@ if (process.env.GATSBY_DRIFT_API) {
});
}

const autoCanonicalURL = false;
if (autoCanonicalURL) {
plugins.push({
resolve: `gatsby-plugin-react-helmet-canonical-urls`,
options: {
siteUrl: process.env.GATSBY_DEFAULT_MAIN_URL,
noTrailingSlash: true,
},
});
}

if (
process.env.ALGOLIA_ADMIN_KEY &&
process.env.GATSBY_ALGOLIA_APP_ID &&
Expand Down
14 changes: 12 additions & 2 deletions src/components/shared/seo/seo.view.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ export const SEO = ({
const currentUrl = slug ? `${docs}/${slug}` : docs;
const currentImage = createMetaImagePath(image, siteUrl, siteImage);

const hrefLangRelAttributeHash = {
en: 'alternate',
es: 'alternate',
};
if (slug && (slug.startsWith('es/') || slug === 'es')) {
hrefLangRelAttributeHash.es = 'canonical';
} else {
hrefLangRelAttributeHash.en = 'canonical';
}

return (
<>
{I18N_CONFIG.hideEsFromRobots &&
Expand Down Expand Up @@ -73,14 +83,14 @@ export const SEO = ({
<link
hrefLang="en"
href={`${siteUrl}/docs${pageTranslations.en.path}`}
rel="alternate"
rel={`${hrefLangRelAttributeHash.en}`}
/>
)}
{pageTranslations && pageTranslations.es !== undefined && (
<link
hrefLang="es"
href={`${siteUrl}/docs${pageTranslations.es.path}`}
rel="alternate"
rel={`${hrefLangRelAttributeHash.es}`}
/>
)}
{pageTranslations && (
Expand Down

0 comments on commit a838a02

Please sign in to comment.