Add canonical page references to documentation
authorJonathan S. Katz <jonathan.katz@excoventures.com>
Sun, 1 Aug 2021 20:04:56 +0000 (16:04 -0400)
committerJonathan S. Katz <jonathan.katz@excoventures.com>
Mon, 21 Mar 2022 15:29:33 +0000 (11:29 -0400)
This sets the documentation pages to use the "/docs/current/"
prefix to be the canonical docs. This follows SEO guidance to
help improve which doc pages show up higher in search, per:

https://developers.google.com/search/docs/advanced/crawling/consolidate-duplicate-urls

Reviewed-by: Magnus Hagander <magnus@hagander.net>
Reviewed-by: Andres Freund <andres@anarazel.de>
pgweb/docs/views.py
templates/docs/docspage.html
templates/docs/index.html

index 9def765707e4596833d9e2d3d4109011bedf3ecb..436e2357d4093666625d282336e2f5c0aa37d8e8 100644 (file)
@@ -128,11 +128,30 @@ def docpage(request, version, filename):
     else:
         contentpreview = ''
 
+    # determine the canonical version of the page
+    # if the doc page is in the current version, then we set it to current
+    # otherwise, check the supported and unsupported versions and find the
+    # last version that the page appeared
+    # we exclude "devel" as development docs are disallowed in robots.txt
+    canonical_version = ""
+    if len(list(filter(lambda v: v.version.current, versions))):
+        canonical_version = "current"
+    else:
+        version_max = None
+        for v in versions:
+            if version_max is None:
+                version_max = v
+            elif v.version.tree > version_max.version.tree:
+                version_max = v
+        if version_max.version.tree > Decimal(0):
+            canonical_version = version_max.display_version()
+
     r = render(request, 'docs/docspage.html', {
         'page': page,
         'supported_versions': [v for v in versions if v.version.supported],
         'devel_versions': [v for v in versions if not v.version.supported and v.version.testing],
         'unsupported_versions': [v for v in versions if not v.version.supported and not v.version.testing],
+        'canonical_version': canonical_version,
         'title': page.title,
         'doc_index_filename': indexname,
         'loaddate': loaddate,
index b23fafbb2d577e5abead9f297bb54c41703b3e55..d2c9341d4a9588e3b1d54eef3a93e1ab8110d9c1 100644 (file)
@@ -15,6 +15,9 @@
 {%if og.description%}  <meta property="og:description" content="{{og.description|truncatewords:"20"}}" />{%endif%}
   <meta property="og:site_name" content="{{og.sitename|default:"PostgreSQL"}}" />{%endif%}
 {%if not page.version.supported%}  <meta name="robots" content="nofollow" />{%endif%}
+{% if canonical_version %}
+  <link rel="canonical" href="https://www.postgresql.org/docs/{{ canonical_version }}/{{ page.file }}" />
+{% endif %}
   <link href="/media/css/fontawesome.css?{{gitrev}}" rel="stylesheet">
   <link rel="stylesheet" href="/media/css/bootstrap-4.4.1.min.css">
   <link rel="shortcut icon" href="/favicon.ico" />
index cfcc2f8f0a32ccc51dfd2b4631faf295e40b6cef..b259967983a64e6d88b57b197b1f0787209054b4 100644 (file)
@@ -27,6 +27,9 @@
        <tr>
         <td>
          <a href="/docs/{{v.numtree}}/index.html">{{v.treestring}}</a>
+    {% if v.current %}
+      / <a href="/docs/current/index.html">Current</a>
+    {% endif %}
         </td>
         <td>
          {%if v.a4pdf or v.uspdf%}