Browse Source

fix SEO of the english articles published in the french site

The canonical link and the noindex meta tag were never emitted on the
english articles published as-is in the french site, because head.html
was checking the language detection of the translation instead of the
current page. Hence, the original articles also advertised the french
copies as their translation through hreflang.

- emit canonical + noindex on the french copies (68 pages)
- only list real translations in hreflang, including the current page
- exclude the french copies from the sitemap and its alternates
- restrict the language detection to regular pages: list pages have
  no file and an empty content, hence they were always detected as copies

Refs #11

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01YNHsWYTXPThURz6TbVS1j3
master
Nicolas Massé 1 month ago
parent
commit
b13180fcda
  1. 32
      layouts/_default/sitemap.xml
  2. 17
      layouts/partials/head.html
  3. 2
      layouts/partials/lang-detection.html
  4. 12
      layouts/partials/real-translations.html

32
layouts/_default/sitemap.xml

@ -0,0 +1,32 @@
{{- /*
-- Based on the Hugo embedded sitemap template (v0.121.2).
--
-- The english articles published as-is in the french site are excluded
-- from the sitemap and from the list of alternates, since they are
-- duplicates of the original articles.
*/ -}}
{{ printf "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>" | safeHTML }}
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xhtml="http://www.w3.org/1999/xhtml">
{{ range $page := .Data.Pages }}
{{- partial "lang-detection.html" . -}}
{{- if and .Permalink (not (.Scratch.Get "en_article_in_fr_site")) -}}
<url>
<loc>{{ .Permalink }}</loc>{{ if not .Lastmod.IsZero }}
<lastmod>{{ safeHTML ( .Lastmod.Format "2006-01-02T15:04:05-07:00" ) }}</lastmod>{{ end }}{{ with .Sitemap.ChangeFreq }}
<changefreq>{{ . }}</changefreq>{{ end }}{{ if ge .Sitemap.Priority 0.0 }}
<priority>{{ .Sitemap.Priority }}</priority>{{ end }}{{ with partial "real-translations.html" . }}{{ range . }}
<xhtml:link
rel="alternate"
hreflang="{{ .Language.LanguageCode }}"
href="{{ .Permalink }}"
/>{{ end }}
<xhtml:link
rel="alternate"
hreflang="{{ $page.Language.LanguageCode }}"
href="{{ $page.Permalink }}"
/>{{ end }}
</url>
{{- end -}}
{{ end }}
</urlset>

17
layouts/partials/head.html

@ -92,8 +92,6 @@
<!-- Deal with the case of english articles published in the french site --> <!-- Deal with the case of english articles published in the french site -->
{{- partial "lang-detection.html" . -}} {{- partial "lang-detection.html" . -}}
{{- if .IsTranslated -}}
{{- range .Translations -}}
{{- if .Scratch.Get "en_article_in_fr_site" -}} {{- if .Scratch.Get "en_article_in_fr_site" -}}
<!-- <!--
-- It's an english article published in the french site -- It's an english article published in the french site
@ -101,12 +99,21 @@
-- --
-- See https://developers.google.com/search/docs/advanced/crawling/consolidate-duplicate-urls -- See https://developers.google.com/search/docs/advanced/crawling/consolidate-duplicate-urls
--> -->
{{- range first 1 .Translations }}
<link rel="canonical" href="{{ .Permalink }}"> <link rel="canonical" href="{{ .Permalink }}">
{{- end }}
<meta name="robots" content="noindex"> <meta name="robots" content="noindex">
{{ else }} {{- else -}}
<!-- Otherwise, just link to the available translations, if any. --> <!--
-- Otherwise, link to the available translations, if any (including the
-- current page, as recommended by Google).
--
-- See https://developers.google.com/search/docs/specialty/international/localized-versions
-->
{{- with partial "real-translations.html" . -}}
{{- range . | append $ }}
<link rel="alternate" hreflang="{{ .Language.Lang }}" href="{{ .Permalink }}" title="{{ .Language.LanguageName }}"> <link rel="alternate" hreflang="{{ .Language.Lang }}" href="{{ .Permalink }}" title="{{ .Language.LanguageName }}">
{{ end -}} {{- end -}}
{{- end -}} {{- end -}}
{{- end -}} {{- end -}}

2
layouts/partials/lang-detection.html

@ -1,4 +1,4 @@
{{ if and .IsTranslated (eq .Lang "fr") }} {{ if and .IsPage .IsTranslated (eq .Lang "fr") }}
<!-- Try to compare files by SH256 fingerprint. --> <!-- Try to compare files by SH256 fingerprint. -->
{{ if and (index .AllTranslations 0).File (index .AllTranslations 1).File }} {{ if and (index .AllTranslations 0).File (index .AllTranslations 1).File }}
{{ $file0 := readFile (index .AllTranslations 0).File.Filename }} {{ $file0 := readFile (index .AllTranslations 0).File.Filename }}

12
layouts/partials/real-translations.html

@ -0,0 +1,12 @@
{{- /*
-- Returns the translations of a page, excluding the english articles
-- published as-is in the french site (they are not real translations).
*/ -}}
{{- $translations := slice -}}
{{- range .Translations -}}
{{- partial "lang-detection.html" . -}}
{{- if not (.Scratch.Get "en_article_in_fr_site") -}}
{{- $translations = $translations | append . -}}
{{- end -}}
{{- end -}}
{{- return $translations -}}
Loading…
Cancel
Save