Add canonical header to help search engines

Fixes #472
This commit is contained in:
Zed 2021-12-30 03:59:11 +01:00
parent 173dd8f016
commit eed4d4033f
3 changed files with 16 additions and 9 deletions

View File

@ -158,7 +158,7 @@ proc getTwitterLink*(path: string; params: Table[string, string]): string =
path = "/search" path = "/search"
if "/search" notin path and query.fromUser.len < 2: if "/search" notin path and query.fromUser.len < 2:
return $(twitter / path ? filterParams(params)) return $(twitter / path)
let p = { let p = {
"f": if query.kind == users: "user" else: "live", "f": if query.kind == users: "user" else: "live",

View File

@ -52,8 +52,10 @@ proc cleanFilename*(filename: string): string =
result &= ".png" result &= ".png"
proc filterParams*(params: Table): seq[(string, string)] = proc filterParams*(params: Table): seq[(string, string)] =
const filter = ["name", "id", "list", "referer", "scroll"] const filter = ["name", "tab", "id", "list", "referer", "scroll"]
toSeq(params.pairs()).filterIt(it[0] notin filter and it[1].len > 0) for p in params.pairs():
if p[1].len > 0 and p[0] notin filter:
result.add p
proc isTwitterUrl*(uri: Uri): bool = proc isTwitterUrl*(uri: Uri): bool =
uri.hostname in twitterDomains uri.hostname in twitterDomains

View File

@ -11,8 +11,7 @@ const
doctype = "<!DOCTYPE html>\n" doctype = "<!DOCTYPE html>\n"
lp = readFile("public/lp.svg") lp = readFile("public/lp.svg")
proc renderNavbar*(cfg: Config, rss: string; req: Request): VNode = proc renderNavbar(cfg: Config; req: Request; rss, canonical: string): VNode =
let twitterPath = getTwitterLink(req.path, req.params)
var path = $(parseUri(req.path) ? filterParams(req.params)) var path = $(parseUri(req.path) ? filterParams(req.params))
if "/status" in path: path.add "#m" if "/status" in path: path.add "#m"
@ -27,14 +26,14 @@ proc renderNavbar*(cfg: Config, rss: string; req: Request): VNode =
icon "search", title="Search", href="/search" icon "search", title="Search", href="/search"
if cfg.enableRss and rss.len > 0: if cfg.enableRss and rss.len > 0:
icon "rss-feed", title="RSS Feed", href=rss icon "rss-feed", title="RSS Feed", href=rss
icon "bird", title="Open in Twitter", href=twitterPath icon "bird", title="Open in Twitter", href=canonical
a(href="https://liberapay.com/zedeus"): verbatim lp a(href="https://liberapay.com/zedeus"): verbatim lp
icon "info", title="About", href="/about" icon "info", title="About", href="/about"
iconReferer "cog", "/settings", path, title="Preferences" iconReferer "cog", "/settings", path, title="Preferences"
proc renderHead*(prefs: Prefs; cfg: Config; titleText=""; desc=""; video=""; proc renderHead*(prefs: Prefs; cfg: Config; titleText=""; desc=""; video="";
images: seq[string] = @[]; banner=""; ogTitle=""; theme=""; images: seq[string] = @[]; banner=""; ogTitle=""; theme="";
rss=""): VNode = rss=""; canonical=""): VNode =
let ogType = let ogType =
if video.len > 0: "video" if video.len > 0: "video"
elif rss.len > 0: "object" elif rss.len > 0: "object"
@ -58,6 +57,9 @@ proc renderHead*(prefs: Prefs; cfg: Config; titleText=""; desc=""; video="";
link(rel="search", type="application/opensearchdescription+xml", title=cfg.title, link(rel="search", type="application/opensearchdescription+xml", title=cfg.title,
href=opensearchUrl) href=opensearchUrl)
if canonical.len > 0:
link(rel="canonical", href=canonical)
if cfg.enableRss and rss.len > 0: if cfg.enableRss and rss.len > 0:
link(rel="alternate", type="application/rss+xml", href=rss, title="RSS feed") link(rel="alternate", type="application/rss+xml", href=rss, title="RSS feed")
@ -117,11 +119,14 @@ proc renderMain*(body: VNode; req: Request; cfg: Config; prefs=defaultPrefs;
if "theme" in req.params: if "theme" in req.params:
theme = toLowerAscii(req.params["theme"]).replace(" ", "_") theme = toLowerAscii(req.params["theme"]).replace(" ", "_")
let canonical = getTwitterLink(req.path, req.params)
let node = buildHtml(html(lang="en")): let node = buildHtml(html(lang="en")):
renderHead(prefs, cfg, titleText, desc, video, images, banner, ogTitle, theme, rss) renderHead(prefs, cfg, titleText, desc, video, images, banner, ogTitle,
theme, rss, canonical)
body: body:
renderNavbar(cfg, rss, req) renderNavbar(cfg, req, rss, canonical)
tdiv(class="container"): tdiv(class="container"):
body body