content/static: use link version in canonical URL path

CanonicalURLPath was incorrectly using requested version.
This change updates CanonicalURLPath and replaces it with
URLPath where requested version is needed.

Change-Id: Ie624c91c258d7ec87e8d3fd61c5b339612904a5b
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/260721
Reviewed-by: Julie Qiu <julie@golang.org>
Trust: Jamal Carvalho <jamal@golang.org>
diff --git a/content/static/html/helpers/_unit_fixed_header.tmpl b/content/static/html/helpers/_unit_fixed_header.tmpl
index aa6da15..e63474a 100644
--- a/content/static/html/helpers/_unit_fixed_header.tmpl
+++ b/content/static/html/helpers/_unit_fixed_header.tmpl
@@ -51,7 +51,7 @@
               <img height="16px" width="16px" src="/static/img/pkg-icon-scale_16x16.svg">
               {{range $i, $e := .Licenses}}
                 {{if $i}}, {{end}}
-                <a href="{{$.CanonicalURLPath}}?tab=licenses#{{.Anchor}}" tabindex="-1">{{$e.Type}}</a>
+                <a href="{{$.URLPath}}?tab=licenses#{{.Anchor}}" tabindex="-1">{{$e.Type}}</a>
               {{else}}
                 <span>None detected</span>
                 <a href="/license-policy" class="Disclaimer-link" tabindex="-1">
@@ -62,13 +62,13 @@
             {{if .Unit.IsPackage}}
               <span class="UnitHeaderFixed-detailItem UnitHeaderFixed-detailItem--lg">
                 <img height="16px" width="16px" src="/static/img/pkg-icon-boxClosed_16x16.svg">
-                <a href="{{$.CanonicalURLPath}}?tab=imports" tabindex="-1">
+                <a href="{{$.URLPath}}?tab=imports" tabindex="-1">
                   {{len .Unit.Imports}} <span>Imports</span>
                 </a>
               </span>
               <span class="UnitHeaderFixed-detailItem UnitHeaderFixed-detailItem--lg">
                 <img height="16px" width="16px" src="/static/img/pkg-icon-boxClosed_16x16.svg">
-                <a href="{{$.CanonicalURLPath}}?tab=importedby" tabindex="-1">
+                <a href="{{$.URLPath}}?tab=importedby" tabindex="-1">
                   {{.ImportedByCount}} <span>Imported by</span>
                 </a>
               </span>
@@ -82,7 +82,7 @@
             <select class="UnitFixedHeader-overflowSelect js-overflowSelect" tabindex="-1">
               {{range .Tabs}}
                 <option
-                  value="{{$.CanonicalURLPath}}?tab={{.Name}}"
+                  value="{{$.URLPath}}?tab={{.Name}}"
                   {{if .Disabled}}
                     disabled
                     data-always-disabled="true"
diff --git a/content/static/html/helpers/_unit_header.tmpl b/content/static/html/helpers/_unit_header.tmpl
index 19ed4a7..15076c6 100644
--- a/content/static/html/helpers/_unit_header.tmpl
+++ b/content/static/html/helpers/_unit_header.tmpl
@@ -74,7 +74,7 @@
           <span class="UnitHeader-detailItem">
             <img height="16px" width="16px" src="/static/img/pkg-icon-scale_16x16.svg">
             {{if .Licenses}}
-              <a href="{{$.CanonicalURLPath}}?tab=licenses">
+              <a href="{{$.URLPath}}?tab=licenses">
                 {{range $i, $e := .Licenses}}
                 {{if $i}}, {{end}} {{$e.Type}}
                 {{end}}
@@ -87,13 +87,13 @@
           {{if .Unit.IsPackage}}
             <span class="UnitHeader-detailItem">
               <img height="16px" width="16px" src="/static/img/pkg-icon-boxClosed_16x16.svg">
-              <a href="{{$.CanonicalURLPath}}?tab=imports">
+              <a href="{{$.URLPath}}?tab=imports">
                 {{len .Unit.Imports}} <span>Imports</span>
               </a>
             </span>
             <span class="UnitHeader-detailItem">
               <img height="16px" width="16px" src="/static/img/pkg-icon-boxClosed_16x16.svg">
-              <a href="{{$.CanonicalURLPath}}?tab=importedby">
+              <a href="{{$.URLPath}}?tab=importedby">
                 {{.ImportedByCount}} <span>Imported by</span>
               </a>
             </span>
diff --git a/internal/frontend/unit.go b/internal/frontend/unit.go
index 0b0f797..7685ba1 100644
--- a/internal/frontend/unit.go
+++ b/internal/frontend/unit.go
@@ -42,6 +42,9 @@
 	// Title is the title of the page.
 	Title string
 
+	// URLPath is the path suitable for links on the page.
+	URLPath string
+
 	// CanonicalURLPath is the representation of the URL path for the details
 	// page, after the requested version and module path have been resolved.
 	// For example, if the latest version of /my.module/pkg is version v1.5.2,
@@ -262,11 +265,16 @@
 		Title:          title,
 		Tabs:           unitTabs,
 		SelectedTab:    tabSettings,
-		CanonicalURLPath: constructPackageURL(
+		URLPath: constructPackageURL(
 			unit.Path,
 			unit.ModulePath,
 			requestedVersion,
 		),
+		CanonicalURLPath: constructPackageURL(
+			unit.Path,
+			unit.ModulePath,
+			linkVersion(unit.Version, unit.ModulePath),
+		),
 		Licenses:        transformLicenseMetadata(unit.Licenses),
 		LastCommitTime:  elapsedTime(unit.CommitTime),
 		DisplayVersion:  displayVersion(unit.Version, unit.ModulePath),