Skip to content

Commit 2159857

Browse files
committed
feat: use raw <a> tags and move htmx logic back to components
hx-boost <3
1 parent 895ce79 commit 2159857

File tree

9 files changed

+18
-43
lines changed

9 files changed

+18
-43
lines changed

pkg/app/app.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,10 @@ func (serv *Server) AddRoute(route string, handler func(app *Server, w http.Resp
7272
log.Info(log_fmt)
7373
} else {
7474
log.Warn(log_fmt, "reason", err)
75-
comp = Error(err.Error(), r.RequestURI)
75+
comp = serv.Template(Error(err.Error(), r.RequestURI))
7676
}
77-
78-
// Already served by handler
79-
if comp == nil {
80-
return
81-
}
82-
if !serv.is_htmx(r) {
83-
comp = serv.Template(comp)
77+
if comp != nil {
78+
comp.Render(context.Background(), w)
8479
}
85-
log.Info(log_fmt)
86-
comp.Render(context.Background(), w)
8780
})
8881
}

pkg/app/error.templ

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ templ Error(message string, path string) {
1010
<p>Error: <span class="text-red-400">{ message }</span></p>
1111
<p>Try refreshing the page or contact the administrator if the problem persists.</p>
1212
</div>
13-
<p>Go back to the <span>@NavButton("/", "home page")</span>.</p>
13+
<p>Go back to the <a href="/">home page</a>.</p>
1414
</div>
1515
}

pkg/app/template.templ

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,11 @@
11
package app
22

33
import (
4-
"time"
5-
"strconv"
64
"app/pkg/app/dev"
5+
"strconv"
6+
"time"
77
)
88

9-
templ NavButton(href string, text string) {
10-
<a
11-
hx-get={ href }
12-
hx-trigger="click"
13-
hx-target="main"
14-
hx-swap="innerHTML"
15-
hx-push-url="true"
16-
class="cursor-pointer"
17-
preload="mouseover"
18-
>
19-
{ text }
20-
</a>
21-
}
22-
239
templ (app *Server) Template(body templ.Component) {
2410
<!DOCTYPE html>
2511
<html lang="en">
@@ -37,12 +23,12 @@ templ (app *Server) Template(body templ.Component) {
3723
/>
3824
@dev.Header
3925
</head>
40-
<body class="mx-auto max-w-xl lg:max-w-4xl flex flex-col gap-8 py-4 px-4" hx-ext="preload">
26+
<body class="mx-auto max-w-xl lg:max-w-4xl flex flex-col gap-8 py-4 px-4" hx-ext="preload" hx-boost="true">
4127
<header class="flex flex-wrap gap-2">
42-
@NavButton("/", "~")
43-
@NavButton("/blog", "blog")
44-
@NavButton("/contact", "contact")
45-
@NavButton("/resume", "resume")
28+
<a href="/">~</a>
29+
<a href="/blog">blog</a>
30+
<a href="/contact">contact</a>
31+
<a href="/resume">resume</a>
4632
<a href="https://github.com/drawbu" target="_blank" rel="noopener">github</a>
4733
<a href="https://linkedin.com/in/clément-boillot" target="_blank" rel="noopener">linkedin</a>
4834
</header>

pkg/routes/blog/article.templ

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package blog
22

3-
import "app/pkg/app"
4-
53
templ articleShow(article article) {
64
<div class="flex flex-col gap-4">
75
<h1>{ article.Title }</h1>
86
<span class="text-gray-500">Written the { article.Date.Format("2 January 2006") } by drawbu</span>
9-
@app.NavButton("/blog", "Back to blog")
7+
<a href="/blog">Back to blog</a>
108
<div id="article" class="flex flex-col gap-4" hx-disable>
119
@templ.Raw(string(article.Content))
1210
</div>

pkg/routes/blog/blog.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ func Handler(serv *app.Server, w http.ResponseWriter, r *http.Request) (templ.Co
4040
article_name, err := url.PathUnescape(r.PathValue("article"))
4141
if err != nil || article_name == "" {
4242
serv.Cache_route(w, r, 3600)
43-
return blog(getSortedArticles(Articles)), nil
43+
return serv.Template(blog(getSortedArticles(Articles))), nil
4444
}
4545

4646
a, ok := Articles[article_name]
4747
if !ok {
4848
return nil, errors.New("Article not found")
4949
}
5050
serv.Cache_route(w, r, 3600)
51-
return articleShow(a), nil
51+
return serv.Template(articleShow(a)), nil
5252
}
5353

5454
func getSortedArticles(articles map[string]article) []article {

pkg/routes/blog/blog.templ

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package blog
22

3-
import "app/pkg/app"
4-
53
templ blog(articles []article) {
64
<div class="flex flex-col gap-4">
75
<h2>Blog</h2>
86
<ul>
97
for _, article := range articles {
108
<li>
119
<span class="mr-2">{ article.Date.Format("2006-01-02") }</span>
12-
@app.NavButton("/blog/"+article.Title, article.Title)
10+
<a href={ templ.SafeURL("/blog/" + article.Title) }>{ article.Title }</a>
1311
</li>
1412
}
1513
</ul>

pkg/routes/contact/contact.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ import (
1010

1111
func Handler(serv *app.Server, w http.ResponseWriter, r *http.Request) (templ.Component, error) {
1212
serv.Cache_route(w, r, 3600)
13-
return contact(), nil
13+
return serv.Template(contact()), nil
1414
}

pkg/routes/resume/resume.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ import (
1010

1111
func Handler(serv *app.Server, w http.ResponseWriter, r *http.Request) (templ.Component, error) {
1212
serv.Cache_route(w, r, 3600)
13-
return resume(), nil
13+
return serv.Template(resume()), nil
1414
}

pkg/routes/root/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func Handler(serv *app.Server, w http.ResponseWriter, r *http.Request) (templ.Co
1313
switch r.URL.Path {
1414
case "/":
1515
serv.Cache_route(w, r, 3600)
16-
return homepage(), nil
16+
return serv.Template(homepage()), nil
1717
default:
1818
serv.Cache_route(w, r, 3600)
1919
http.FileServerFS(static.Files).ServeHTTP(w, r)

0 commit comments

Comments
 (0)