Setting noindex on non-pages

I was looking for a simple way to discard “archives” (sections, tags and taxonomy terms) pages as well as homepage pagination from being indexed, because I found that Google often returns archives pages linking to relevant content, not to the relevant content itself.

There is this post showing how to do it, but then this other explains why the solution is flimsy. After a couple of tries, here is how I did it (tested with Hugo 0.146):

{{ $robots := `<meta name="robots" content="noindex, follow">` }}

{{ if or (eq .Kind "page")
         (and (eq .Kind "home") (not (in .RelPermalink "/page/")) )
}}
{{ $robots = `
<meta name="robots" content="index,follow">
<meta name="bingbot" content="index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1">
<meta name="googlebot" content="index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1">
` }}
{{ end }}

{{ $robots | safeHTML }}

Depending on whether your section index pages contain more content than simply a listing of children pages, you may want to add another eq .Kind “section” statement in there.

The trick is to detect pagination through looking at the URL (for /page/) instead of calling the problematic .Paginator object (which now raises an error on objects that don’t support it, aka page kind).

The Permalink and RelPermalink methods for a Page object don’t include the value of the pagination path configuration, which is page by default. See issue #4507.

So I don’t understand how this could possibly work:

in .RelPermalink "/page/"

I would use this approach instead:
https://discourse.gohugo.io/t/pagination-using-pager-values-within-the-head-element/50340