Skip to content

Commit a838a02

Browse files
committed
Fix canonical attribute when using hreflang
1 parent c9cfb42 commit a838a02

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

gatsby-config.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,6 @@ const shouldAnnouncementBannerBeShown = false;
1212

1313
const plugins = [
1414
'gatsby-plugin-react-helmet',
15-
{
16-
resolve: `gatsby-plugin-react-helmet-canonical-urls`,
17-
options: {
18-
siteUrl: process.env.GATSBY_DEFAULT_MAIN_URL,
19-
noTrailingSlash: true,
20-
},
21-
},
2215
'gatsby-transformer-sharp',
2316
'gatsby-plugin-sharp',
2417
'gatsby-plugin-catch-links',
@@ -171,6 +164,17 @@ if (process.env.GATSBY_DRIFT_API) {
171164
});
172165
}
173166

167+
const autoCanonicalURL = false;
168+
if (autoCanonicalURL) {
169+
plugins.push({
170+
resolve: `gatsby-plugin-react-helmet-canonical-urls`,
171+
options: {
172+
siteUrl: process.env.GATSBY_DEFAULT_MAIN_URL,
173+
noTrailingSlash: true,
174+
},
175+
});
176+
}
177+
174178
if (
175179
process.env.ALGOLIA_ADMIN_KEY &&
176180
process.env.GATSBY_ALGOLIA_APP_ID &&

src/components/shared/seo/seo.view.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ export const SEO = ({
4040
const currentUrl = slug ? `${docs}/${slug}` : docs;
4141
const currentImage = createMetaImagePath(image, siteUrl, siteImage);
4242

43+
const hrefLangRelAttributeHash = {
44+
en: 'alternate',
45+
es: 'alternate',
46+
};
47+
if (slug && (slug.startsWith('es/') || slug === 'es')) {
48+
hrefLangRelAttributeHash.es = 'canonical';
49+
} else {
50+
hrefLangRelAttributeHash.en = 'canonical';
51+
}
52+
4353
return (
4454
<>
4555
{I18N_CONFIG.hideEsFromRobots &&
@@ -73,14 +83,14 @@ export const SEO = ({
7383
<link
7484
hrefLang="en"
7585
href={`${siteUrl}/docs${pageTranslations.en.path}`}
76-
rel="alternate"
86+
rel={`${hrefLangRelAttributeHash.en}`}
7787
/>
7888
)}
7989
{pageTranslations && pageTranslations.es !== undefined && (
8090
<link
8191
hrefLang="es"
8292
href={`${siteUrl}/docs${pageTranslations.es.path}`}
83-
rel="alternate"
93+
rel={`${hrefLangRelAttributeHash.es}`}
8494
/>
8595
)}
8696
{pageTranslations && (

0 commit comments

Comments
 (0)