Skip to content

Commit 50e8fb8

Browse files
set page canonical link (#1055)
1 parent c5280e6 commit 50e8fb8

3 files changed

Lines changed: 19 additions & 0 deletions

File tree

pkg/app/http.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,11 @@ func (h *Handler) servePage(w http.ResponseWriter, r *http.Request) {
688688
Content(v)
689689
}),
690690
Title().Text(page.Title()),
691+
If(page.canonicalLink != "", func() UI {
692+
return Link().
693+
Rel("canonical").
694+
Href(resolveOGResource(h.Domain, page.canonicalLink))
695+
}),
691696
Range(h.Preconnect).Slice(func(i int) UI {
692697
if resource := parseHTTPResource(h.Preconnect[i]); resource.URL != "" {
693698
return resource.toLink().Rel("preconnect")

pkg/app/page.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ type Page interface {
6565

6666
// Set the Twitter card.
6767
SetTwitterCard(v TwitterCard)
68+
69+
// Set the page's canonical link.
70+
SetCanonicalLink(format string, v ...any)
6871
}
6972

7073
type requestPage struct {
@@ -79,6 +82,7 @@ type requestPage struct {
7982
preloads []Preload
8083
loadingLabel string
8184
image string
85+
canonicalLink string
8286
width int
8387
height int
8488
twitterCardMap map[string]string
@@ -173,6 +177,12 @@ func (p *requestPage) SetTwitterCard(v TwitterCard) {
173177
p.twitterCardMap = v.toMap()
174178
}
175179

180+
func (p *requestPage) SetCanonicalLink(format string, v ...any) {
181+
if canonicalLink := FormatString(format, v...); canonicalLink != "" {
182+
p.canonicalLink = p.resolveURL(canonicalLink)
183+
}
184+
}
185+
176186
type browserPage struct {
177187
resolveURL func(string) string
178188
}
@@ -283,6 +293,9 @@ func (p browserPage) SetTwitterCard(v TwitterCard) {
283293
}
284294
}
285295

296+
func (p browserPage) SetCanonicalLink(format string, v ...any) {
297+
}
298+
286299
func (p browserPage) metaByName(v string) Value {
287300
meta := Window().
288301
Get("document").

pkg/app/page_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,5 @@ func testPage(t *testing.T, p Page) {
5757
require.NotZero(t, h)
5858

5959
p.SetTwitterCard(TwitterCard{Card: "summary"})
60+
p.SetCanonicalLink("/canon")
6061
}

0 commit comments

Comments
 (0)