Skip to content

Commit

Permalink
feat(SEO): correct canonical for public pages
Browse files Browse the repository at this point in the history
  • Loading branch information
Gusted committed Jul 19, 2021
1 parent 1bd76b9 commit 4729b35
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 30 deletions.
7 changes: 4 additions & 3 deletions handlers/core/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ func GetDocs(c *fiber.Ctx) error {
}

return c.Render("core/docs", fiber.Map{
"Title": title,
"User": u,
"content": content,
"Title": title,
"User": u,
"content": content,
"Canonical": "docs",
})
}
5 changes: 3 additions & 2 deletions handlers/core/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ func NotFound(c *fiber.Ctx) error {
})
}
return c.Render("err", fiber.Map{
"User": u,
"Title": "Page not found",
"User": u,
"Title": "Page not found",
"Canonical": "404",
})
}
1 change: 1 addition & 0 deletions handlers/core/modlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ func GetModLog(c *fiber.Ctx) error {
"RemovedStyles": removedStyles,
"User": u,
"Title": "Mod Log",
"Canonical": "modlog",
})
}
13 changes: 7 additions & 6 deletions handlers/core/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ func Search(c *fiber.Ctx) error {
}

return c.Render("core/search", fiber.Map{
"Title": "Search",
"User": u,
"Styles": s,
"Value": q,
"Root": c.OriginalURL() == "/search",
"Sort": fv,
"Title": "Search",
"User": u,
"Styles": s,
"Value": q,
"Root": c.OriginalURL() == "/search",
"Sort": fv,
"Canonical": "search",
})
}
17 changes: 9 additions & 8 deletions handlers/style/explore.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,14 @@ func GetExplore(c *fiber.Ctx) error {
}

return c.Render("core/explore", fiber.Map{
"Title": "Explore",
"User": u,
"Styles": s,
"Sort": fv,
"PageMax": maxPages,
"PageNow": pageNow,
"PageBack": pageNow - 1,
"PageNext": pageNow + 1,
"Title": "Explore",
"User": u,
"Styles": s,
"Sort": fv,
"PageMax": maxPages,
"PageNow": pageNow,
"PageBack": pageNow - 1,
"PageNext": pageNow + 1,
"Canonical": "explore",
})
}
1 change: 1 addition & 0 deletions handlers/style/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,6 @@ func GetStylePage(c *fiber.Ctx) error {
"DailyHistory": dailyHistory,
"TotalHistory": totalHistory,
"Reviews": reviews,
"Canonical": "style/" + id,
})
}
3 changes: 2 additions & 1 deletion handlers/user/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ func LoginGet(c *fiber.Ctx) error {
return c.Redirect("/account", fiber.StatusSeeOther)
}
arguments := fiber.Map{
"Title": "Login",
"Title": "Login",
"Canonical": "login",
}

if r := c.Query("r"); r != "" {
Expand Down
9 changes: 5 additions & 4 deletions handlers/user/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ func Profile(c *fiber.Ctx) error {
}*/

return c.Render("user/profile", fiber.Map{
"Title": profile.Name() + "'s profile",
"User": u,
"Profile": profile,
"Styles": styles,
"Title": profile.Name() + "'s profile",
"User": u,
"Profile": profile,
"Styles": styles,
"Canonical": "user/" + p,
})
}
3 changes: 2 additions & 1 deletion handlers/user/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ func RegisterGet(c *fiber.Ctx) error {
}

return c.Render("user/register", fiber.Map{
"Title": "Register",
"Title": "Register",
"Canonical": "register",
})
}

Expand Down
3 changes: 2 additions & 1 deletion handlers/user/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ func RecoverGet(c *fiber.Ctx) error {
return c.Redirect("/account", fiber.StatusSeeOther)
}
return c.Render("user/recover", fiber.Map{
"Title": "Reset",
"Title": "Reset",
"Canonical": "recover",
})
}

Expand Down
10 changes: 7 additions & 3 deletions modules/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,13 @@ func getEnv(name, fallback string) string {

// OAuthURL returns the proper callback URL depending on the environment.
func OAuthURL() string {
return BaseURL() + "/api/callback/"
}

// BaseURL returns the proper BaseURL.
func BaseURL() string {
if Production {
return "https://userstyles.world/api/callback/"
return "https://userstyles.world"
}

return "http://localhost" + Port + "/api/callback/"
return "http://localhost" + Port
}
7 changes: 7 additions & 0 deletions modules/templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ func New() *html.Engine {
}`
})

engine.AddFunc("canonical", func(url interface{}) template.HTML {
if url == nil {
return template.HTML(config.BaseURL())
}
return template.HTML(config.BaseURL() + "/" + url.(string))
})

if !config.Production {
engine.Reload(true)
}
Expand Down
2 changes: 1 addition & 1 deletion views/partials/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<meta name="color-scheme" content="dark light">
<meta name="description" content="A new site to explore userstyles.">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="canonical" href="https://userstyles.world">
<link rel="canonical" href="{{ canonical .Canonical }}">
<link rel="stylesheet" href="/css/main.css?v={{ $commit }}" type="text/css" media="screen">
<link rel="mask-icon" href="/mascot.svg" color="blue">
<link rel="icon" href="/favicon.ico">
Expand Down

0 comments on commit 4729b35

Please sign in to comment.