Open platform to collect and prioritize feedback
enhancement: make show more a link (SEO) (#667)
Guilherme Oenning2a97bb55282d
diff --git a/app/handlers/apiv1/invite.go b/app/handlers/apiv1/invite.goindex d925655b7..cfa0af93a 100644--- a/app/handlers/apiv1/invite.go+++ b/app/handlers/apiv1/invite.go@@ -25,7 +25,7 @@ func SendSampleInvite() web.HandlerFunc {"subject": input.Model.Subject,"message": markdown.Full(input.Model.Message),})- err := c.Services().Emailer.Send(c, "invite_email", email.Params{}, c.Tenant().Name, to)+ err := c.Services().Emailer.Send(&c, "invite_email", email.Params{}, c.Tenant().Name, to)if err != nil {return c.Failure(err)}diff --git a/app/handlers/post.go b/app/handlers/post.goindex f584997a3..4e6800aaa 100644--- a/app/handlers/post.go+++ b/app/handlers/post.go@@ -9,6 +9,8 @@ import (// Index is the default home pagefunc Index() web.HandlerFunc {return func(c web.Context) error {+ c.SetCanonicalLink("")+posts, err := c.Services().Posts.Search(c.QueryParam("query"),c.QueryParam("view"),diff --git a/app/middlewares/tenant.go b/app/middlewares/tenant.goindex b0f62e5bb..a4446b9f7 100644--- a/app/middlewares/tenant.go+++ b/app/middlewares/tenant.go@@ -1,7 +1,6 @@package middlewaresimport (- "fmt""net/http""github.com/getfider/fider/app/models"@@ -65,7 +64,7 @@ func MultiTenant() web.MiddlewareFunc {baseURL := c.TenantBaseURL(tenant)if baseURL != c.BaseURL() {link := baseURL + c.Request.URL.RequestURI()- c.Response.Header().Set("Link", fmt.Sprintf("<%s>; rel=\"canonical\"", link))+ c.SetCanonicalLink(link)}}}diff --git a/app/pkg/web/context.go b/app/pkg/web/context.goindex 9500b240e..5ceecc135 100644--- a/app/pkg/web/context.go+++ b/app/pkg/web/context.go@@ -5,6 +5,7 @@ import ("encoding/json""fmt""net/http"+ "net/url""strconv""time"@@ -152,7 +153,7 @@ func (ctx *Context) Enqueue(task worker.Task) {}//Tenant returns current tenant-func (ctx Context) Tenant() *models.Tenant {+func (ctx *Context) Tenant() *models.Tenant {tenant, ok := ctx.Get(tenantContextKey).(*models.Tenant)if ok {return tenant@@ -394,7 +395,7 @@ func (ctx *Context) ActiveTransaction() *dbx.Trx {}//BaseURL returns base URL-func (ctx Context) BaseURL() string {+func (ctx *Context) BaseURL() string {address := ctx.Request.URL.Scheme + "://" + ctx.Request.URL.Hostname()if ctx.Request.URL.Port() != "" {@@ -543,7 +544,7 @@ func (ctx *Context) TenantAssetsURL(path string, a ...interface{}) string {}// LogoURL return the full URL to the tenant-specific logo URL-func (ctx Context) LogoURL() string {+func (ctx *Context) LogoURL() string {if ctx.Tenant() != nil && ctx.Tenant().LogoID > 0 {return ctx.TenantAssetsURL("/images/200/%d", ctx.Tenant().LogoID)}@@ -551,9 +552,31 @@ func (ctx Context) LogoURL() string {}// FaviconURL return the full URL to the tenant-specific favicon URL-func (ctx Context) FaviconURL() string {+func (ctx *Context) FaviconURL() string {if ctx.Tenant() != nil && ctx.Tenant().LogoID > 0 {return ctx.TenantAssetsURL("/images/50/%d", ctx.Tenant().LogoID)}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)+ 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+ }+ link = baseURL + link+ ctx.Response.Header().Set("Link", fmt.Sprintf("<%s%s>; rel=\"canonical\"", baseURL, link))+ } else {+ ctx.Set("Canonical-BaseURL", u.Scheme+"://"+u.Host)+ }++ ctx.Response.Header().Set("Link", fmt.Sprintf("<%s>; rel=\"canonical\"", link))+ }+}diff --git a/app/pkg/web/context_test.go b/app/pkg/web/context_test.goindex cc6b2136f..e21492b60 100644--- a/app/pkg/web/context_test.go+++ b/app/pkg/web/context_test.go@@ -190,3 +190,39 @@ func TestTenantAssetsURL_MultiHostMode(t *testing.T) {Expect(ctx.TenantAssetsURL("/assets/main.js")).Equals("http://theavengers.assets-fider.io/assets/main.js")Expect(ctx.TenantAssetsURL("/assets/main.css")).Equals("http://theavengers.assets-fider.io/assets/main.css")}++func TestCanonicalURL_SameDomain(t *testing.T) {+ RegisterT(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.SetCanonicalLink("/some-url")+ Expect(ctx.Response.Header().Get("Link")).Equals(`<http://theavengers.test.fider.io:3000/some-url>; rel="canonical"`)++ ctx.SetCanonicalLink("/some-other-url")+ Expect(ctx.Response.Header().Get("Link")).Equals(`<http://theavengers.test.fider.io:3000/some-other-url>; rel="canonical"`)++ 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"`)+}++func TestCanonicalURL_DifferentDomain(t *testing.T) {+ RegisterT(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.SetCanonicalLink("")+ Expect(ctx.Response.Header().Get("Link")).Equals(`<http://feedback.theavengers.com>; rel="canonical"`)++ ctx.SetCanonicalLink("/some-other-url")+ Expect(ctx.Response.Header().Get("Link")).Equals(`<http://feedback.theavengers.com/some-other-url>; rel="canonical"`)++ ctx.SetCanonicalLink("page-b/abc.html")+ Expect(ctx.Response.Header().Get("Link")).Equals(`<http://feedback.theavengers.com/page-b/abc.html>; rel="canonical"`)+}diff --git a/public/components/VoteCounter.scss b/public/components/VoteCounter.scssindex db9df8df1..809206d5e 100644--- a/public/components/VoteCounter.scss+++ b/public/components/VoteCounter.scss@@ -15,16 +15,17 @@text-align: center;margin: 0 auto;padding: 0 0;- color: $gray-darker;+ color: $gray-dark;.icon {- color: $gray-darker;+ color: $gray-dark;margin-bottom: -3px;}}button.m-disabled {@include disabled();+ cursor: not-allowed;}button.m-voted,diff --git a/public/pages/Home/components/ListPosts.scss b/public/pages/Home/components/ListPosts.scssindex 1be36e48c..ce5e208ed 100644--- a/public/pages/Home/components/ListPosts.scss+++ b/public/pages/Home/components/ListPosts.scss@@ -40,6 +40,7 @@.c-post-list-show-more {cursor: pointer;+ font-weight: 500;color: $main-color;margin-left: 55px;}\ No newline at end of filediff --git a/public/pages/Home/components/PostsContainer.tsx b/public/pages/Home/components/PostsContainer.tsxindex 38463e36b..b87f21729 100644--- a/public/pages/Home/components/PostsContainer.tsx+++ b/public/pages/Home/components/PostsContainer.tsx@@ -91,11 +91,14 @@ export class PostsContainer extends React.Component<PostsContainerProps, PostsCothis.changeFilterCriteria({ limit: (this.state.limit || 30) + 10 }, false);};- private canShowMore = (): boolean => {- return this.state.posts ? this.state.posts.length >= (this.state.limit || 30) : false;+ private getShowMoreLink = (): string | undefined => {+ if (this.state.posts && this.state.posts.length >= (this.state.limit || 30)) {+ return querystring.set("limit", (this.state.limit || 30) + 10);+ }};public render() {+ const showMoreLink = this.getShowMoreLink();return (<><div className="row">@@ -132,10 +135,10 @@ export class PostsContainer extends React.Component<PostsContainerProps, PostsCoemptyText={"No results matched your search, try something different."}/>{this.state.loading && <Loader />}- {this.canShowMore() && (- <h5 className="c-post-list-show-more" onTouchEnd={this.showMore} onClick={this.showMore}>+ {showMoreLink && (+ <a href={showMoreLink} className="c-post-list-show-more" onTouchEnd={this.showMore} onClick={this.showMore}>View more posts- </h5>+ </a>)}</>);diff --git a/public/services/querystring.spec.ts b/public/services/querystring.spec.tsindex 2a1134567..80c608ab6 100644--- a/public/services/querystring.spec.ts+++ b/public/services/querystring.spec.ts@@ -39,6 +39,18 @@ import navigator from "./navigator";});});+[+ { qs: "?name=John&age=30", name: "age", value: 60, expected: "http://example.com?name=John&age=60" },+ { qs: "", name: "age", value: 50, expected: "http://example.com?age=50" },+ { qs: "?age=2", name: "age", value: 21, expected: "http://example.com?age=21" }+].forEach(x => {+ test(`set(${x.name}, ${x.value}) should return ${x.expected}`, () => {+ navigator.url = () => `http://example.com${x.qs}`;+ const newQueryString = qs.set(x.name, x.value);+ expect(newQueryString).toEqual(x.expected);+ });+});+[{ object: {}, expected: "" },{ object: undefined, expected: "" },diff --git a/public/services/querystring.ts b/public/services/querystring.tsindex 53ac919d5..bd08ca32d 100644--- a/public/services/querystring.ts+++ b/public/services/querystring.ts@@ -4,6 +4,17 @@ export const getNumber = (name: string): number | undefined => {return parseInt(get(name), 10) || undefined;};+export const set = (name: string, value: any): string => {+ const uri = navigator.url();+ const re = new RegExp("([?&])" + name + "=.*?(&|$)", "i");+ if (uri.match(re)) {+ return uri.replace(re, "$1" + name + "=" + value + "$2");+ } else {+ const separator = uri.indexOf("?") !== -1 ? "&" : "?";+ return uri + separator + name + "=" + value;+ }+};+export const get = (name: string): string => {name = name.replace(/[\[\]]/g, "\\$&");const regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)");