Skip to content

Commit bb6b4cd

Browse files
committed
feat: Refactor telemetry and update canonical URL logic
Refactors the telemetry JavaScript to remove fallback URLs, ensuring `data-url` is the sole source for view and like counts. Updates the `PreferredCanonical` function to construct the canonical URL directly from the post's path, simplifying the logic and removing reliance on `LangCanonical` metadata. Also includes minor CSS updates for filter properties and templating fixes for URL generation.
1 parent f837dc0 commit bb6b4cd

8 files changed

Lines changed: 22 additions & 38 deletions

public/main.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -575,11 +575,9 @@ async function telemetry() {
575575
try {
576576
const viewEls = document.querySelectorAll('[data-view-count]');
577577
if (viewEls.length > 0) {
578-
// Prefer canonical URL if present (server-rendered), otherwise use current location as default.
579-
const canonicalEl = document.querySelector('link[rel="canonical"]');
580-
const defaultUrl = (canonicalEl && canonicalEl.href) ? canonicalEl.href : window.location.href;
578+
// Use element's data-url only (do NOT fall back to canonical or current location).
581579
const urls = Array.from(viewEls)
582-
.map(el => el.getAttribute('data-url') || defaultUrl)
580+
.map(el => el.getAttribute('data-url'))
583581
.filter(Boolean);
584582
// Deduplicate URLs and record views for each
585583
const uniqueUrls = [...new Set(urls)];
@@ -620,7 +618,8 @@ async function hydrateCounts() {
620618
// Views
621619
const viewEls = document.querySelectorAll('[data-view-count]');
622620
for (const el of viewEls) {
623-
const url = el.getAttribute('data-url') || window.location.href;
621+
const url = el.getAttribute('data-url');
622+
if (!url) continue;
624623
// keep placeholder until hydrated
625624
el.textContent = 'views ....';
626625
try {
@@ -638,7 +637,8 @@ async function hydrateCounts() {
638637
// Likes
639638
const likeButtons = document.querySelectorAll('[data-like-button]');
640639
for (const btn of likeButtons) {
641-
const url = btn.getAttribute('data-url') || window.location.href;
640+
const url = btn.getAttribute('data-url');
641+
if (!url) continue;
642642
const span = btn.querySelector('[data-like-count]');
643643
if (span) span.textContent = 'like ...';
644644
try {

view/component_blog_header.templ

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package view
33
templ BlogHeader(m *Metadata) {
44
<header class="flex justify-between items-center p-4 border-2 border-black rounded-lg mb-6">
55
if m.Language != "en" {
6-
<a class="text-2xl font-bold" href={templ.SafeURL("/"+m.Language+"/")}>GoSuda</a>
6+
<a class="text-2xl font-bold" href={ templ.SafeURL("/" + m.Language + "/") }>GoSuda</a>
77
} else {
88
<a class="text-2xl font-bold" href="/">GoSuda</a>
99
}

view/component_blog_header_templ.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

view/component_gosuda_blog_post.templ

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,7 @@ import (
66
)
77

88
func PreferredCanonical(meta *types.Metadata) string {
9-
if meta == nil {
10-
return ""
11-
}
12-
if meta.LangCanonical != nil {
13-
if v, ok := meta.LangCanonical["en"]; ok && v != "" {
14-
return v
15-
}
16-
}
17-
return meta.Canonical
9+
return "https://gosuda.org" + meta.Path
1810
}
1911

2012
templ GosudaBlogPost(m *Metadata, doc *types.Document, post *types.Post) {

view/component_gosuda_blog_post_templ.go

Lines changed: 7 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

view/component_head.templ

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ templ Head(m *Metadata) {
5151
}
5252
}
5353
if m.Language == "en" {
54-
<link rel="alternate" type="application/rss+xml" href={m.BaseURL+"/feed.rss"}/>
54+
<link rel="alternate" type="application/rss+xml" href={ m.BaseURL + "/feed.rss" }/>
5555
} else {
56-
<link rel="alternate" type="application/rss+xml" href={m.BaseURL+"/"+m.Language+"/feed.rss"}/>
56+
<link rel="alternate" type="application/rss+xml" href={ m.BaseURL + "/" + m.Language + "/feed.rss" }/>
5757
}
5858
<link rel="apple-touch-icon" sizes="180x180" href="/assets/apple-touch-icon.png"/>
5959
<link rel="icon" type="image/png" sizes="32x32" href="/assets/favicon-32x32.png"/>

view/component_head_templ.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

view/index.templ

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ type Metadata struct {
2121
}
2222

2323
type Alternate struct {
24-
Default string
25-
Versions []KV
24+
Default string
25+
Versions []KV
2626
}
2727

2828
type KV struct {

0 commit comments

Comments
 (0)