Skip to content

Commit 1697840

Browse files
Add a self-referencing canonical tag to every page (#586)
* Add a self-referencing canonical tag to every page Pages without canonical_url had no canonical tag, so Google treats tracking-parameter URLs (?utm_source=..., ?ref=...) as duplicates. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Point paginated list pages' canonical at themselves /blog/2 and the other pager pages took .Permalink, which is page 1. The blog post list moves into a shared partial so head.html and blog-list.html paginate the same collection. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent b2f1197 commit 1697840

3 files changed

Lines changed: 18 additions & 5 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{{ $posts := where .Data.Pages "Params.hidden" "!=" true }}
2+
{{/* Append homepage to blogposts and hide it later - hack to get 1 less post on the first page, to show a
3+
larger featured post */}}
4+
{{ if not (or (eq .Type "authors") (eq .Type "tags")) }}
5+
{{ $posts = slice site.Home | append $posts }}
6+
{{ end }}
7+
{{ return $posts }}

‎themes/haystack/layouts/partials/blog-list.html‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,8 @@ <h2 class="section-title">
109109
{{ end }}
110110
{{/* Blog posts grid */}}
111111
<div class="blog-grid blog-grid-static">
112-
{{/* Append homepage to blogposts and hide it later - hack to get 1 less post on the first page, to show a
113-
larger featured post */}}
114-
{{ if not (or (eq .Type "authors") (eq .Type "tags")) }}
115-
{{ $blogPosts = slice site.Home | append $blogPosts }}
116-
{{ end }}
112+
{{/* Same list the canonical tag in head.html paginates, so both use one paginator */}}
113+
{{ $blogPosts = partial "blog-list-posts" . }}
117114
{{ $paginatedBlogPosts := .Paginate $blogPosts }}
118115

119116
{{/* Check if there is a next/previous page */}}

‎themes/haystack/layouts/partials/head.html‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@
2626
{{/* Canonical url */}}
2727
{{ if .Params.canonical_url }}
2828
<link rel="canonical" href="{{ .Params.canonical_url }}" />
29+
{{ else }}
30+
{{ $canonical := .Permalink }}
31+
{{/* Paginated lists: point each pager page at itself, not at page 1. Args must match the .Paginate call in the body. */}}
32+
{{ if eq .Layout "release-notes" }}
33+
{{ $canonical = (.Paginate .Data.Pages 12).URL | absURL }}
34+
{{ else if or (eq .Layout "blog") (and (eq .Kind "term") (in (slice "authors" "tags") .Type)) }}
35+
{{ $canonical = (.Paginate (partial "blog-list-posts" .)).URL | absURL }}
36+
{{ end }}
37+
<link rel="canonical" href="{{ $canonical | replaceRE "/$" "" }}" />
2938
{{ end }}
3039

3140
{{/* Cookie management */}}

0 commit comments

Comments
 (0)