Open platform to collect and prioritize feedback
enhancement: set canonical link on HTML (#675)
Guilherme Oenninge2d33e6a268a
diff --git a/app/handlers/post.go b/app/handlers/post.goindex a51ec1f25..169a4490d 100644--- a/app/handlers/post.go+++ b/app/handlers/post.go@@ -9,7 +9,7 @@ import (// Index is the default home pagefunc Index() web.HandlerFunc {return func(c web.Context) error {- c.SetCanonicalLink("")+ c.SetCanonicalURL("")posts, err := c.Services().Posts.Search(c.QueryParam("query"),diff --git a/app/middlewares/tenant.go b/app/middlewares/tenant.goindex a4446b9f7..c7954e25a 100644--- a/app/middlewares/tenant.go+++ b/app/middlewares/tenant.go@@ -64,7 +64,7 @@ func MultiTenant() web.MiddlewareFunc {baseURL := c.TenantBaseURL(tenant)if baseURL != c.BaseURL() {link := baseURL + c.Request.URL.RequestURI()- c.SetCanonicalLink(link)+ c.SetCanonicalURL(link)}}}diff --git a/app/middlewares/tenant_test.go b/app/middlewares/tenant_test.goindex 6c24b4996..f59089f07 100644--- a/app/middlewares/tenant_test.go+++ b/app/middlewares/tenant_test.go@@ -94,7 +94,7 @@ func TestMultiTenant_CanonicalHeader(t *testing.T) {}{{"http://avengers.test.fider.io/",- "<http://feedback.theavengers.com/>; rel=\"canonical\"",+ "http://feedback.theavengers.com/",false,},{@@ -109,12 +109,12 @@ func TestMultiTenant_CanonicalHeader(t *testing.T) {},{"http://avengers.test.fider.io/posts",- "<http://feedback.theavengers.com/posts>; rel=\"canonical\"",+ "http://feedback.theavengers.com/posts",false,},{"http://avengers.test.fider.io/posts?q=1",- "<http://feedback.theavengers.com/posts?q=1>; rel=\"canonical\"",+ "http://feedback.theavengers.com/posts?q=1",false,},{@@ -131,14 +131,17 @@ func TestMultiTenant_CanonicalHeader(t *testing.T) {if testCase.isAjax {server.AddHeader("Accept", "application/json")}- status, response := server.++ var canonicalURL string+ status, _ := server.WithURL(testCase.input).Execute(func(c web.Context) error {+ canonicalURL, _ = c.Get("Canonical-URL").(string)return c.Ok(web.Map{})})Expect(status).Equals(http.StatusOK)- Expect(response.HeaderMap.Get("Link")).Equals(testCase.output)+ Expect(canonicalURL).Equals(testCase.output)}}diff --git a/app/pkg/web/context.go b/app/pkg/web/context.goindex d46041d00..0d09d6b61 100644--- a/app/pkg/web/context.go+++ b/app/pkg/web/context.go@@ -560,24 +560,23 @@ func (ctx *Context) FaviconURL() string {return ctx.GlobalAssetsURL("/favicon.ico")}-// SetCanonicalLink sets the canonical link on the HTTP Response Headers-func (ctx *Context) SetCanonicalLink(link string) {- u, err := url.Parse(link)+// SetCanonicalURL sets the canonical link on the HTTP Response Headers+func (ctx *Context) SetCanonicalURL(rawurl string) {+ u, err := url.Parse(rawurl)if err == nil {if u.Host == "" {baseURL, ok := ctx.Get("Canonical-BaseURL").(string)if !ok {baseURL = ctx.BaseURL()}- if len(link) > 0 && link[0] != '/' {- link = "/" + link+ if len(rawurl) > 0 && rawurl[0] != '/' {+ rawurl = "/" + rawurl}- link = baseURL + link- ctx.Response.Header().Set("Link", fmt.Sprintf("<%s%s>; rel=\"canonical\"", baseURL, link))+ rawurl = baseURL + rawurl} else {ctx.Set("Canonical-BaseURL", u.Scheme+"://"+u.Host)}- ctx.Response.Header().Set("Link", fmt.Sprintf("<%s>; rel=\"canonical\"", link))+ ctx.Set("Canonical-URL", rawurl)}}diff --git a/app/pkg/web/context_test.go b/app/pkg/web/context_test.goindex e21492b60..9e4f99613 100644--- a/app/pkg/web/context_test.go+++ b/app/pkg/web/context_test.go@@ -196,17 +196,17 @@ func TestCanonicalURL_SameDomain(t *testing.T) {ctx := newGetContext("http://theavengers.test.fider.io:3000", nil)- ctx.SetCanonicalLink("")- Expect(ctx.Response.Header().Get("Link")).Equals(`<http://theavengers.test.fider.io:3000>; rel="canonical"`)+ ctx.SetCanonicalURL("")+ Expect(ctx.Get("Canonical-URL")).Equals(`http://theavengers.test.fider.io:3000`)- ctx.SetCanonicalLink("/some-url")- Expect(ctx.Response.Header().Get("Link")).Equals(`<http://theavengers.test.fider.io:3000/some-url>; rel="canonical"`)+ ctx.SetCanonicalURL("/some-url")+ Expect(ctx.Get("Canonical-URL")).Equals(`http://theavengers.test.fider.io:3000/some-url`)- ctx.SetCanonicalLink("/some-other-url")- Expect(ctx.Response.Header().Get("Link")).Equals(`<http://theavengers.test.fider.io:3000/some-other-url>; rel="canonical"`)+ ctx.SetCanonicalURL("/some-other-url")+ Expect(ctx.Get("Canonical-URL")).Equals(`http://theavengers.test.fider.io:3000/some-other-url`)- ctx.SetCanonicalLink("page-b/abc.html")- Expect(ctx.Response.Header().Get("Link")).Equals(`<http://theavengers.test.fider.io:3000/page-b/abc.html>; rel="canonical"`)+ ctx.SetCanonicalURL("page-b/abc.html")+ Expect(ctx.Get("Canonical-URL")).Equals(`http://theavengers.test.fider.io:3000/page-b/abc.html`)}func TestCanonicalURL_DifferentDomain(t *testing.T) {@@ -214,15 +214,15 @@ func TestCanonicalURL_DifferentDomain(t *testing.T) {ctx := newGetContext("http://theavengers.test.fider.io:3000", nil)- ctx.SetCanonicalLink("http://feedback.theavengers.com/some-url")- Expect(ctx.Response.Header().Get("Link")).Equals(`<http://feedback.theavengers.com/some-url>; rel="canonical"`)+ ctx.SetCanonicalURL("http://feedback.theavengers.com/some-url")+ Expect(ctx.Get("Canonical-URL")).Equals(`http://feedback.theavengers.com/some-url`)- ctx.SetCanonicalLink("")- Expect(ctx.Response.Header().Get("Link")).Equals(`<http://feedback.theavengers.com>; rel="canonical"`)+ ctx.SetCanonicalURL("")+ Expect(ctx.Get("Canonical-URL")).Equals(`http://feedback.theavengers.com`)- ctx.SetCanonicalLink("/some-other-url")- Expect(ctx.Response.Header().Get("Link")).Equals(`<http://feedback.theavengers.com/some-other-url>; rel="canonical"`)+ ctx.SetCanonicalURL("/some-other-url")+ Expect(ctx.Get("Canonical-URL")).Equals(`http://feedback.theavengers.com/some-other-url`)- ctx.SetCanonicalLink("page-b/abc.html")- Expect(ctx.Response.Header().Get("Link")).Equals(`<http://feedback.theavengers.com/page-b/abc.html>; rel="canonical"`)+ ctx.SetCanonicalURL("page-b/abc.html")+ Expect(ctx.Get("Canonical-URL")).Equals(`http://feedback.theavengers.com/page-b/abc.html`)}diff --git a/app/pkg/web/renderer.go b/app/pkg/web/renderer.goindex cebb014eb..861ab257c 100644--- a/app/pkg/web/renderer.go+++ b/app/pkg/web/renderer.go@@ -186,6 +186,9 @@ func (r *Renderer) Render(w io.Writer, name string, props Props, ctx *Context) {m["__favicon"] = ctx.FaviconURL()m["__contextID"] = ctx.ContextID()m["__currentURL"] = ctx.Request.URL.String()+ if canonicalURL := ctx.Get("Canonical-URL"); canonicalURL != nil {+ m["__canonicalURL"] = canonicalURL+ }m["__tenant"] = ctx.Tenant()m["__props"] = props.Datadiff --git a/app/pkg/web/renderer_test.go b/app/pkg/web/renderer_test.goindex 4210d3cdc..41024299f 100644--- a/app/pkg/web/renderer_test.go+++ b/app/pkg/web/renderer_test.go@@ -50,6 +50,17 @@ func TestRenderer_Tenant(t *testing.T) {compareRendererResponse(buf, "/app/pkg/web/testdata/tenant.html", ctx)}+func TestRenderer_WithCanonicalURL(t *testing.T) {+ RegisterT(t)++ buf := new(bytes.Buffer)+ ctx := newGetContext("https://demo.test.fider.io:3000/", nil)+ ctx.SetCanonicalURL("http://feedback.demo.org")+ renderer := web.NewRenderer(&models.SystemSettings{}, noop.NewLogger())+ renderer.Render(buf, "index.html", web.Props{}, ctx)+ compareRendererResponse(buf, "/app/pkg/web/testdata/canonical.html", ctx)+}+func TestRenderer_Props(t *testing.T) {RegisterT(t)diff --git a/app/pkg/web/testdata/basic.html b/app/pkg/web/testdata/basic.htmlindex 107baa71c..6b869aa70 100755--- a/app/pkg/web/testdata/basic.html+++ b/app/pkg/web/testdata/basic.html@@ -7,6 +7,7 @@<link rel="icon" type="image/x-icon" href="https://demo.test.fider.io:3000/favicon.ico">+<link rel="stylesheet" href="https://demo.test.fider.io:3000/assets/css/file1.css" />diff --git a/app/pkg/web/testdata/canonical.html b/app/pkg/web/testdata/canonical.htmlnew file mode 100755index 000000000..e818f2e5f--- /dev/null+++ b/app/pkg/web/testdata/canonical.html@@ -0,0 +1,55 @@+<!DOCTYPE html>+<html>+<head>+ <meta charset="utf-8"/>+ <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0">+ <meta http-equiv="X-UA-Compatible" content="IE=edge" />+ <link rel="icon" type="image/x-icon" href="https://demo.test.fider.io:3000/favicon.ico">++ <link rel="canonical" href="http://feedback.demo.org" />++++ <link rel="stylesheet" href="https://demo.test.fider.io:3000/assets/css/file1.css" />++++ <title>Fider</title>+ <meta name="description" content="" />+ <meta property="og:title" content="Fider" />+ <meta property="og:description" content="" />+ <meta property="og:type" content="website" />+ <meta property="og:url" content="https://demo.test.fider.io:3000/" />+ <meta property="og:image" content="https://getfider.com/images/logo-100x100.png">++</head>+<body>+ <noscript class="container">+ <div class="c-segment">+ <h2>Please enable JavaScript</h2>+ <p>This website relies on JavaScript-based technologies and services.</p>+ <p>Please enable JavaScript and then reload the page.</p>+ </div>+ </noscript>++++ <div id="root"></div><div id="root-modal"></div><div id="root-toastify"></div>++ <script id="server-data" type="application/json">++ {"__assets":{"CSS":["/assets/css/file1.css"],"JS":["/assets/js/file1.js","/assets/js/file2.js"]},"__canonicalURL":"http://feedback.demo.org","__contextID":"CONTEXT_ID","__currentURL":"https://demo.test.fider.io:3000/","__favicon":"https://demo.test.fider.io:3000/favicon.ico","__logo":"https://getfider.com/images/logo-100x100.png","__props":null,"__settings":{"baseURL":"https://demo.test.fider.io:3000","buildTime":"","compiler":"","domain":"","environment":"","globalAssetsURL":"https://demo.test.fider.io:3000","googleAnalytics":"","hasLegal":false,"mode":"","oauth":[],"tenantAssetsURL":"https://demo.test.fider.io:3000","version":""},"__tenant":null,"__title":"Fider"}++ </script>+ <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=default,fetch" crossorigin="anonymous"></script>++++ <script src="https://demo.test.fider.io:3000/assets/js/file1.js" crossorigin="anonymous"></script>++ <script src="https://demo.test.fider.io:3000/assets/js/file2.js" crossorigin="anonymous"></script>++++</body>+</html>diff --git a/app/pkg/web/testdata/chunk.html b/app/pkg/web/testdata/chunk.htmlindex 0af8183bf..7d17fa170 100644--- a/app/pkg/web/testdata/chunk.html+++ b/app/pkg/web/testdata/chunk.html@@ -7,6 +7,7 @@<link rel="icon" type="image/x-icon" href="https://demo.test.fider.io:3000/favicon.ico">+<link rel="stylesheet" href="https://demo.test.fider.io:3000/assets/css/file1.css" />diff --git a/app/pkg/web/testdata/props.html b/app/pkg/web/testdata/props.htmlindex 0e46ccdc3..e157174e8 100755--- a/app/pkg/web/testdata/props.html+++ b/app/pkg/web/testdata/props.html@@ -7,6 +7,7 @@<link rel="icon" type="image/x-icon" href="https://demo.test.fider.io:3000/favicon.ico">+<link rel="stylesheet" href="https://demo.test.fider.io:3000/assets/css/file1.css" />diff --git a/app/pkg/web/testdata/tenant.html b/app/pkg/web/testdata/tenant.htmlindex d856f28bf..2d1b7d634 100755--- a/app/pkg/web/testdata/tenant.html+++ b/app/pkg/web/testdata/tenant.html@@ -7,6 +7,7 @@<link rel="icon" type="image/x-icon" href="https://demo.test.fider.io:3000/favicon.ico">+<link rel="stylesheet" href="https://demo.test.fider.io:3000/assets/css/file1.css" />diff --git a/app/pkg/web/testdata/user.html b/app/pkg/web/testdata/user.htmlindex 7b35de5ad..993d4628e 100755--- a/app/pkg/web/testdata/user.html+++ b/app/pkg/web/testdata/user.html@@ -7,6 +7,7 @@<link rel="icon" type="image/x-icon" href="https://demo.test.fider.io:3000/favicon.ico">+<link rel="stylesheet" href="https://demo.test.fider.io:3000/assets/css/file1.css" />diff --git a/views/base.html b/views/base.htmlindex 0fd04ecd0..64d1708db 100644--- a/views/base.html+++ b/views/base.html@@ -5,6 +5,9 @@<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0"><meta http-equiv="X-UA-Compatible" content="IE=edge" /><link rel="icon" type="image/x-icon" href="{{ .__favicon }}">+ {{ if .__canonicalURL }}+ <link rel="canonical" href="{{ .__canonicalURL }}" />+ {{ end }}{{ if .__assets }}{{range .__assets.CSS}}<link rel="stylesheet" href="{{ $.__settings.globalAssetsURL }}{{ . }}" />